EXERCISE-3 MSD LAB PROGRAMS
EXERCISE-3 MSD LAB PROGRAMS
Exercise -3 (Node.js)
a).Write a program to show the workflow of JavaScript code executable by creating web
server in Node.js.
b). Write a Node.js module to show the workflow of Modularization of Node application
c). Write a Node.js program to show the workflow of restarting a Node application.
PROCEDURE STEPS:
Step 1: Download and install the Node,js software and set the path for Node,js S/W.
Step2: Open the VS CODE and create a file httpserver.js
Step3: Open the Power Shell Terminal(… on the menu bar) in VS CODE.
Step 4: Run the node.js file httpserver.js using the node command.
(PS D:\MSD LAB\Exercise-3\>node httpserver.js press enter button then server starts.
Step 5: Open the web browser and enter URL as http://localhost:3000 and press enter
button then output will be displayed on the webpage.
Step 6: To stop the server from the Power Shell Terminal, press Ctrl + C
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"); // Importing the http module
var server = http.createServer((req,res) => { // Creating server
res.write( ` // Sending the response
<!DOCTYPE html>
<html>
<head>
<title>Node.js Server</title>
</head>
<body bgcolor="pink">
<br><br><center><h1><font color="blue" size="25">Hello World! Welcome To CSE!
</font></h1>
<h1><font color="red" size="20">St.Ann's College of Engineering&Technology!
</font></h1>
<h2><font color="green" size="15">This page is served by a simple Node.js server.
</font></h2>
</center></body>
</html>
`);
res.end(); // res.end() function is used to end the response process.
});
server.listen(3000); // server listening to port 3000 of your computer.
console.log("Server started... Running on localhost:3000");
OUTPUT: 3(a)
Exercise -3(b): Node.js
AIM:
Write a Node.js module to show the workflow of Modularization of Node application
PROCEDURE STEPS:
Step 1: Create a first file NodeModule.js in VS CODE.
Step 2: Create a second file ModuleApp.js in VS CODE.
Step 3: Run the ModuleApp.js file using the node command.
(PS D:\MSD LAB\Exercise-3\>node ModuleApp.js press enter button then server starts.
Step 4: Open the web browser and enter URL as http://localhost:2000 and press enter
button then output will be displayed on the webpage.
Step 5: To stop the server from the Power Shell Terminal, press Ctrl + C
ModuleApp.js
const http = require("http");
var nodemodule =require("./NodeModule");
var server =http.createServer((request, response) => {
result = nodemodule.authenticateUser("admin", "admin");
// The res.writeHead() method is the status code where 200 means it is OK, while the second
argument is an object containing the response headers.
response.writeHead(200, { "Content-Type": "text/html" });
response.write( `
<!DOCTYPE html>
<html>
<head> <title>Node.js </title>
</head>
<body style="background-color:#F0E68C;">
<br><br>
<center><h1><font color="red" size="30"><u>Modular Programming in Node.js </u>
</font></h1>
<h1><font color="blue" size="25">Hello World! Welcome To CSE!</font></h1>
<h1><font color="red" size="24">St.Ann's College of Engineering&Technology!
</font></h1>
<h2><font color="green" size="15">This page is served by a simple Node.js server.
</font></h2></center></body></html>
`);
response.end("<center><h1>" + result +"</h1> </center>");
console.log("Request Received");
});
server.listen(2000);
console.log("Server is running at port 2000");
OUTPUT: 3(b)
Exercise -3(c) : Node.js
AIM:
Write a Node.js program to show the workflow of restarting a Node application.
PROCEDURE STEPS:
Step 1: Create a file restart.js in VS CODE
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:\MSD LAB\Exercise -3> 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".
OUTPUT: 3(c)
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 .
MODIFIED OUTPUT: