Exercise-6 MST Programs
Exercise-6 MST Programs
6.a
Course Name: Node.js
Module Name: How to use Node.js
AIM: Verify how to execute different functions successfully in the Node.js platform.
function tester() {
alert("Hello! Welcome to CSE!");
}
tester();
Step 4: Create a file fun2.js and Run the fun2.js using the node command.
PS D:\NodeJS\Exercise-6\6.a >node fun2.js press enter button then output displayed
Program2: fun2.js
function tester() {
var message;
if (confirm("Press a button!")) {
message = "You pressed OK!";
} else {
message = "You pressed Cancel!";
}
console.log(message);
}
tester();
Note: Functions that perform browser interaction are not compatible with Node.js. You
can try out other functions as well and observe the output.
Step5: Create a file fun3.js and Run the fun3.js using the node command.
PS D:\NodeJS\Exercise-6\6.a >node fun3.js press enter button then output displayed
Program3: Fun3.js
6.b
Course Name: Node.js
Module Name: Create a web server in Node.js
AIM: Write a program to show the workflow of JavaScript code executable by creating
web server in Node.js.
SOURCE CODE:
//”http” module includes classes, methods and events to create Node.js http server.
// The require() function will return an object, function, property or any other JavaScript type
const http = require("http");
var server = http.createServer((req, res) => {
res.write("Hello World! Welcome to CSE!");
res.end(); // res.end() function is used to end the response process.
});
server.listen(3000); //Create a server that listens on port 3000 of your computer.
console.log("Server started... Running on localhost:3000");
6.b OUTPUT:
6.c
Course Name: Node.js
Module Name: Modular programming in Node.js
AIM: Write a Node.js module to show the workflow of Modularization of Node application.
NodeModule.js
6.c OUTPUT:
6.d
Course Name: Node.js
Module Name: Restarting Node Application
AIM: Write a program to show the workflow of restarting a Node application.
Step 2: Open the Power Shell Terminal(… on the menu bar) in VS CODE
Step 3: To Restart Node application, we can use the Nodemon tool. To install it in the
application, run the below command.
PS D:\NodeJS\> npm install nodemon -g
Once the 'nodemon' is installed in the machine, the Node.js server code can be
executed by replacing the command "node" with "nodemon".
Step 4: Run the restart.js using the nodemon command.
Step 5: Open a browser and navigate to the URL http://localhost:3000 and display the
output.
6.d OUTPUT
Thus, the 'nodemon' starts the application in watch mode and restarts the
application when any change is detected.
Now open the restart.js application code and do changes in the code .
Step 6: Open a browser and navigate to the URL http://localhost:3000 and display the
modified output.
6.d OUTPUT
6.e
Course Name: Node.js
Module Name: File Operations
AIM: Create a text file src.txt and add the following data to it. Mongo, Express, Angular,
Node.
SOURCE CODE:
src.txt
// Create a src.txt file
Welecome to III B.Tech CSE
Client-side Technologies:
HTML
CSS
JS
Anjular
React
Server-side Technologies:
Node
Express
Databases:
MySQL
MongoDB
read.js
// Opening, Reading and Closing a file.
const fs = require("fs");
console.log("Open an existing file");
fs.open('src.txt', 'r', (err, fd) => //Open an existing file
{
if (err) {
return console.error(err);
}
console.log("File opened successfully!");
console.log("Read a file");
fs.readFile('src.txt', 'utf8', (err, fileData) => // Reading a file
{
if (err) throw err;
console.log(fileData);
});
fs.close(fd, function(err) { // Close the opened file.
if (err) {
console.log(err);
}
console.log("File closed successfully.");
});
});
write.js
// Creating and Writing a file.
var fs = require("fs");
fs.writeFile('input.html', 'utf8', function(err) {
if (err) {
console.error(err);
}
else
console.log("File created successfully!");
});
const fileContent="Welcome to File System in Node.js";
fs.writeFile('input.html', fileContent, function(err){
if (err) {
console.error(err);
}
else
console.log("File conent written successfully!");
});
append.js
// Append a content to a file
const fs = require('fs');
const appendContent="\nNode.js is a Runtime Environment";
fs.appendFile('input.html', appendContent,function(err){
if (err) {
console.error(err);
}
else
console.log("File content appended successfully!");
});