Creating web server with Node.js


In this post we will show about how to create node.js server with http module.http module includes classes, methods and events to create Node.js http server. Before starting create server in node.js let us to see part of Node.js application. Node.js include three type of module.
  1. Core Module
  2. Local Module
  3. Third party Module
To create server in node.js please see code bellow:
var http = require("http");
http.createServer(function (request, response) {
   response.writeHead(200, {'Content-Type': 'text/plain'});
   response.end('Hello World\n');
}).listen(8081);
console.log('Server running at http://127.0.0.1:8081/');

  • var http = require("http") is used to import core module http of node.js for create server.
  • http.createServer() is used to create server that listen with port 8081.
Now execute the main.js to start server as bellow

Next open browser and type localhost:8081 or 127.0.0.1:8081

Share this

Related Posts

Previous
Next Post »