30-minutes with Node.js – P1

I’ve read about Node.js and finally had a chance to test it out. After 30 minutes, I found it easy to use and very powerful, and would definitely explore some more in the future. This blog documents my short experiment with Node.js

Node.js is a runtime as as well as a library. It is based on Google V8 Engine and is what Chrome uses to run JavaScript. Node.js allows one to create back-end app with JavaScript.

1. 00:01:00 Insteall Node.js

http://nodejs.org/

2. 00:02:00 Test node at the commandline prompt

Open an terminal and type “node”. If node can not be found, then close all terminals and open a new terminal. Test again. If for some reason it still does not work , then add the path to nodejs (C:\Program Files\nodejs on Windows 8) to your system path

3. 00:03:00 Start reading tutorial for beginner

http://www.nodebeginner.org/

Create a helloworld.js

console.log("hello World");

At the commandline, change directory to where helloworld.js is and

node helloworld.js

4. 00:30:00 Created a webserver using Node.js

Create a server.js file

var http=require("http")
function start(){
   function onRequest(request, response){
       console.log("Request received");
       response.writeHead(200,{"Content-Type":"text/plain"});
       response.write("Hello World");
       response.end();
   }
   http.createServer(onRequest).listen(8888);
   console.log("Server has started.");
}
exports.start = start;

Create an index.js file

var server = require("./server");
server.start();

Then start the server

node index.js

12 Comments

Leave a Reply to nexvan Cancel reply