Tuesday, August 7, 2012

How to use node.js?


Node.js is a platform built on Chrome's JavaScript run-time for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices. Node.js is a server-side version of JavaScript. That means all the things all them cool things about JavaScript apply here.

What makes Node any different from the rest?
Node is evented I/O for V8 JavaScript. V8 is Google’s super fast JavaScript implementation that’s used in their Chrome browser. JavaScript’s ability to pass around closures makes event-based programming dead simple.

Node is not strictly for web development. You can think of Node as a framework for server development of any kind. With Node you can build an IRC server, a chat server, or HTTP server as done in following hello world example. 

You can use node.js to create light weight web server. See the following example from node.js tutorial site:

var http = require('http');
  http.createServer(function (req, res) {
      res.writeHead(200, {'Content-Type': 'text/plain'});
      res.write("Dynamic contents...");
      res.end('Hello World\n');
  }).listen(1337, "");

console.log('Server running at');
Above code is java script so you can write your dynamic code generation logic in java script and start using it in no time.

You will have to download and setup node.js from link:http://nodejs.org/ for Windows or whatever platform you prefer to use.

Start the web server using command:
node hello.js

To test if above sample works open your browser and hit url:

You should see familiar Hello World message.

node.js Download: http://nodejs.org/
node.js Hello World Example: http://www.theprojectspot.com/tutorial-post/Node-js-for-beginners-part-1-hello-world/2
More detailed discussion: http://net.tutsplus.com/tutorials/javascript-ajax/this-time-youll-learn-node-js/

No comments:

Post a Comment

Example code to connect node.js REST service with Eureka discovery service

Eureka is Spring based discovery service developed by Netflix. To know how Eureka is used at Netflix visit: https://github.com/Netflix/e...