Download location for the Node.js?
http://nodejs.org/#download
For Microsoft family, you need to download “node.exe Windows executable”.
Writing your first hello world program with Node.js
After downloading the executable to a folder, follow the below steps for writing your first program with Node.js
- Copy/paste the below code to a file and save it as HelloWorld.js file
// These are like using statement in C#
var http = require(“http”);
var url = require(“url”);
// This creates the web server object.
http.createServer(function(request, response) {
response.writeHead(200, {“Content-Type”: “text/plain”});
response.write(“Hello World”);
response.end();
// Specify the port number for listening.
}).listen(81);
//
console.log(“Your web server started”);
2) Assuming that you have saved your HelloWorld.js file and Node.exe under the folder C:\Node, open the command prompt and navigate to the path C:\Node. Now run the following command at the prompt:
Your web server should running now and you should be seeing the message “Your web server started”.
3)Now open your browser and type in the following URL:
You should be seeing the response from your HelloWorld server.
Yay! You have just created your very first application with Node.js. For more information about Node.js visit http://nodejs.org/