Pages

Create a WebServer


A tutorial which i am going to show how to create a simpe webserver for node.js. Here you can download node.js and install in system.

Open a notepad and copy the following code snippet



var http = require('http');  // include the http module

http.createServer(function (req, res) {

res.writeHead(200, {'Content-Type': 'text/plain'}); //setting respose

res.end('Hello World\n');

}).listen(1337, '127.0.0.1'); // setup the port.you can the port to 8080

console.log('Server running at http://127.0.0.1:1337/'); // printing in server console

Save it as myserver.js in any drive. Now goto-> run -> cmd. Change the directory to your myserver.js path and type "node myserver.js" in the command prompt like below

myserver.js in d: drive and my web server running fine. Test the server in any browser and type 127.0.0.1:1337

OUTPUT:

HelloWorld