Ex No: 9 WEB SERVER USING NODE.JS
Aim: To implement a server that reads and sends a HTML page using node.js.
Procedure
Step1: Install Node Package Manager (npm) in the system.
Step2: Initialize Node.js using "npm init -y" in the terminal.
Step3: Create a file “server.js” to implement the server.
Step4: Include "http", "url" and "fs" modules read the html file and send it to the client.
Step6: Create a HTML file “index.html" which is read by the "server.js".
Step7: Run the code using "node server.js".
Source Code

server.js


const http = require('http');
const fs = require('fs');
const url = require('url');

// Create a server
http.createServer( function (request, response) {  
   // Get the File Name
   var pathname = url.parse(request.url).pathname;
   console.log("Request for " + pathname + " received.");
   
   // Read the requested file content 
   fs.readFile(pathname.substring(1), function (err, data) {
      if (err) {
         console.log(err);
      } else {
         response.writeHead(200, {'Content-Type': 'text/html'});	
         console.log(pathname.substring(1) +" sent to the client");
         //Create a response witht the file content
         response.write(data.toString());		
      }      
      // Send the response 
      response.end();
   });   
}).listen(5000);

// Display Server Link on Console
console.log('Server running at http://127.0.0.1:5000/');

Result: Thus node.js server is implemented to read and send "index.html" file to the client.