Monday, 23 June 2014

NodeJS Hello World Example

Background

As Wiki says

“Node.js is a packaged compilation of Google’s V8 JavaScript engine, the libuv platform abstraction layer, and a core library, which is itself primarily written in JavaScript.”

Node.js applications are designed to maximize throughput and efficiency, using non-blocking I/O and asynchronous events. Node.js applications run single-threaded, although Node.js uses multiple threads for file and network events. Node.js is commonly used for real time applications due to its asynchronous nature, allowing applications to display information faster for users without the need for refreshing.

How it Works?

Node.js is a platform built on Chrome's JavaScript runtime 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.

Getting Started

  1. First step would be to download the Node.js application. You can download the installer from the official site. Add the executable to the class path. If in windows add it to the %PATH% environment variable. If using linux you can set in your .profile file and export it.
  2. To test whether it is properly added to the PATH you can simply open the command prompt and type node -v  : It should give you the version you are using.

  3. Next lets get directly to the code. Open your favorite text editor (Notepad, gedit, vi, emacs .. whatever you like) and paste in the following code

        var http = require('http');
        http.createServer(function (req, res) {
          res.writeHead(200, {'Content-Type': 'text/plain'});
          res.end('Hello World\n');
        }).listen(1337, "127.0.0.1");
        console.log('Server running at http://127.0.0.1:1337/');
    


    Then save the file  with .js extension. I am saving it as hw.js (representing helloworld.js).
  4. And that's it. Lets execute our code. Again open command prompt where your hw.js file is located and run command `node hw.js`. This would start your server.

  5. Finally you can check in your browser by typing the URL http://localhost:1337. You should see "Hello World!"

It is that simple to write applications in NodeJS.

Node.js modules

Node.js has lots of built in modules that you can use. Like on above example we used http module to create a server.

To include a module you need to use the require() function with the name of the module. Eg.
  • var http = require('http'); 

You can even write your own modules and use. Eg. consider following module definition -

exports.getCurrentDate= function () {
    return Date();
}; 


save it as dateModule.js and then you can use it as -

var dM = require('./dateModule');
console.log(dM.getCurrentDate());


assuming the main file is in same directory as module file  dateModule.js

NOTE : You need to use exports keyword to make methods and properties available outside the it's module file.

Installing NPM

NPM is Node Package manager. You will need this too for installing different modules.  So download the NPM zip file from node.js site  . Extract it in the same folder as that of node installer . Also remember we had added that directory in the PATH env variable so even npm command should be valid in console. You can try that out by typing npm in the cmd. You should see the usage. You can also try installing a package.

npm install express -g


You can see the module installed in NodeJS\node_modules where NodeJS is the directory which has the node installer in it.

Important Links

No comments:

Post a Comment

t> UA-39527780-1 back to top