HI WELCOME TO Sirees

Node.js Web Module

Leave a Comment

What is Web Server

Web Server is a software program that handles HTTTP requests sent by HTTP clients like web browsers, and returns web pages in response to the clients. Web servers usually respond with html documents along with images, style sheets and scripts.
Most of the web server support server side scripts using scripting language or redirect to application server which perform the specific task of getting data from database, perform complex logic etc. and then sends a result to the HTTP client through the Web server.
Apache web server is one of the most commonly used web server. It is an open source project.

Web Application Architecture

A web application can be divided in 4 layers:
  • Client Layer: The Client layer contains web browsers, mobile browsers or applications which can make HTTP request to the web server.
  • Server Layer: The Server layer contains Web server which can intercepts the request made by clients and pass them the response.
  • Business Layer: The business layer contains application server which is utilized by web server to do required processing. This layer interacts with data layer via data base or some external programs.
  • Data Layer: The Data layer contains databases or any source of data.
Node.js web layer

Creating Web Server using Node.js

Node.js provides http module which can be used to create either HTTP client of server. Create a js file named server.js having the following 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
  1. <html>  
  2. <head>  
  3. <title>Sample Page</title>  
  4. </head>  
  5. <body>  
  6. Hello World!  
  7. </body>  
  8. </html>  
Now open the Node.js command prompt and run the following code:
node server.js
Node.js web module1
Open http://127.0.0.1:8081/index.htm in any browser and see the below result.
Node.js web module2

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.