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, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

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 "http://127.0.0.1:1337/".

Saturday 8 September 2012

Setup NodeJS on Windows box NodeJS

Setup NodeJS on Windows box

This post destine for beginners how want start learning programming in NodeJS.
First, we need to download the latest version of NodeJS from its official website nodejs.org .
Install *.msi file and follow the install wizard.

After install we should add path of NodeJS root folder to environment variables in order to run NodeJS from inside any folder.

Go to "Computer" > "Properties" > "Advanced system settings".

Click "Environment Variables..." button.

There is appear "Environment Variables" form. Go to "System variables" and click to "New..." button. In the "New System  Variable" from add the following:
 Variable name: NODE_PATH
 Variable value: C:\Program Files\nodejs\
save it.
Edit "Path" system variable.
add end of this value ;%NODE_PATH%
Now NodeJS is ready to use.