Monday, 24 September 2012

Write First Web Server

In the previous blog we set up NodeJS to our windows box. We have a look the example provided in the NodeJs home page.
This simple web server written in Node responds with "Hello World" for every request.

// include "http" module
var http = require('http');
// create http server
http.createServer(function (req, res) {
    // set response header with status code 200 and content-type
    res.writeHead(200, {
        'Content-Type': 'text/plain'
    // wirte some data to response stream
    res.end('Hello World\n');
    // set listener port to 1337, it can be changed
}).listen(1337, '');
console.log('Server running at');

To run the above server code, put the code into a file "example.js" and execute it with the node program in the CMD:
After success executing server code, the result can accessible in the web browser with URL "".

No comments:

Post a Comment