|
| 1 | +/** |
| 2 | + * Web Module |
| 3 | + * |
| 4 | + * Web Server is a software program that handles HTTTP requests sent by HTTP clients |
| 5 | + * like web browsers, and returns web pages in response to the clients. Web servers |
| 6 | + * usually respond with html documents along with images, style sheets and scripts. |
| 7 | + * |
| 8 | + * |
| 9 | + * ------------------------------ |
| 10 | + * Web Application Architecture |
| 11 | + * ------------------------------ |
| 12 | + * |
| 13 | + * 1. Client Layer: The Client layer contains web browsers, mobile browsers or applications |
| 14 | + * which can make HTTP request to the web server. |
| 15 | + * |
| 16 | + * 2. Server Layer: The Server layer contains Web server which can intercepts the request made |
| 17 | + * by clients and pass them the response. |
| 18 | + * |
| 19 | + * 3. Business Layer: The business layer contains application server which is utilized by web |
| 20 | + * server to do required processing. This layer interacts with data layer via data base or some external programs. |
| 21 | + * |
| 22 | + * 4. Data Layer: The Data layer contains databases or any source of data. |
| 23 | + * |
| 24 | + */ |
| 25 | + |
| 26 | + |
| 27 | +var http = require('http'); |
| 28 | +var fs = require('fs'); |
| 29 | +var url = require('url'); |
| 30 | + |
| 31 | +// Create a server |
| 32 | +http.createServer( function (request, response) { |
| 33 | + // Parse the request containing file name |
| 34 | + var pathname = url.parse(request.url).pathname; |
| 35 | + |
| 36 | + // Print the name of the file for which request is made. |
| 37 | + console.log("Request for " + pathname + " received."); |
| 38 | + |
| 39 | + // Read the requested file content from file system |
| 40 | + fs.readFile(pathname.substr(1), function (err, data) { |
| 41 | + if (err) { |
| 42 | + console.log(err); |
| 43 | + // HTTP Status: 404 : NOT FOUND |
| 44 | + // Content Type: text/plain |
| 45 | + response.writeHead(404, {'Content-Type': 'text/html'}); |
| 46 | + }else{ |
| 47 | + //Page found |
| 48 | + // HTTP Status: 200 : OK |
| 49 | + // Content Type: text/plain |
| 50 | + response.writeHead(200, {'Content-Type': 'text/html'}); |
| 51 | + |
| 52 | + // Write the content of the file to response body |
| 53 | + response.write(data.toString()); |
| 54 | + } |
| 55 | + // Send the response body |
| 56 | + response.end(); |
| 57 | + }); |
| 58 | +}).listen(4200); |
| 59 | +// Console will print the message |
| 60 | +console.log('Server running at http://localhost:4200/index.html'); |
0 commit comments