home PYTHONJAVA
 

Node.js Web Module


What is Web Server

Web Server is a software program that handles HTTTP requests sent by HTTP customers like internet browsers, and returns site pages in light of the customers. Web servers for the most part react with html reports alongside pictures, templates and scripts.

Most of the web server support server side contents utilizing scripting language or redirect to application server which play out the particular undertaking of getting information from database, perform complex rationale and so on and after that sends an outcome to the HTTP client through the Web server.

Apache web server is a one amongst the most ordinarily utilized web server. It is an open source project.

Web Application Architecture

A web application can be separated in 4 layers:

  • Client Layer: The Client layer contains internet browsers, portable programs or applications which can Create HTTP solicitation to the web server.
  • Server Layer: The Server layer contains Web server which can captures the solicitation made by customers and pass them the response.
  • Business Layer: The business layer contains application server which is used by web server to do required preparing. This layer interfaces with information layer by means of information base or some outer programs.
  • Data Layer: The Data layer contains databases or any source of data.

Creating Web Server utilizing Node.js

Node.js gives http module which can be utilized to Create either HTTP client of server. Create a js record named server.js having the accompanying code:

code:

  1. var http = require('http');  
  2. var fs = require('fs');  
  3. var url = require('url');  
  4.   
  5. // Create a server  
  6. http.createServer( function (request, response) {    
  7.    // Parse the request containing file name  
  8.    var pathname = url.parse(request.url).pathname;  
  9.      
  10.    // Print the name of the file for which request is made.  
  11.    console.log("Request for " + pathname + " received.");  
  12.      
  13.    // Read the requested file content from file system  
  14.    fs.readFile(pathname.substr(1), function (err, data) {  
  15.       if (err) {  
  16.          console.log(err);  
  17.          // HTTP Status: 404 : NOT FOUND  
  18.          // Content Type: text/plain  
  19.          response.writeHead(404, {'Content-Type': 'text/html'});  
  20.       }else{      
  21.          //Page found       
  22.          // HTTP Status: 200 : OK  
  23.          // Content Type: text/plain  
  24.          response.writeHead(200, {'Content-Type': 'text/html'});      
  25.            
  26.          // Write the content of the file to response body  
  27.          response.write(data.toString());         
  28.       }  
  29.       // Send the response body   
  30.       response.end();  
  31.    });     
  32. }).listen(8081);  
  33. // Console will print the message  
  34. console.log('Server running at http://127.0.0.1:8081/');  

Next, create an html file named index.html having the following code in the same directory where you created server.js






welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.