Home Scripting Node.js Node.js Tutorial: Definition, Advantages, Terminology, Installation, Examples

Node.js Tutorial: Definition, Advantages, Terminology, Installation, Examples

    4 1335


    What is node.js?

    Node.js is a server side scripting platform just like php, python or ruby.
    Php depends on Apache or nginx web servers for handling http request and response, on the other hand Node.js has its own http server library allowing us to have more control on the web server.

    Definition of Node

    Node.js is built on Chrome’s JavaScript runtime V8 for developing 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.

    What is a scalable network application?
    An application is said to be scalable when it is efficient and practical when applied to large situations(eg: large input data set, a large number of outputs or users). If the application fails when quantity increases, it is not scalable.

    What is an event-driven, non-blocking I/O model?
    When you are using a traditional web server like apache, when a request is received, it processes that request and goes to next request. if that request takes time, the server will wait for it and then moves to the next request. In order to process more requests the web server uses multiple threads-which is more resource consuming.
    In event-driven or non-blocking I/O model the web server accepts the request, then goes back to service next web request(it does not block that I/O operation). When the original request is completed, it comes back in the processing queue and then the results are sent to the browser. This makes it highly efficient and scalable. This is what made nginx web server very popular. nginx uses a scalable event-driven (asynchronous) architecture. The same technology is used in node.js in a much efficient way.
    To Handle 10,000 simultaneous requests node.js uses a few mb of RAM whereas Apache would probably consume hundreds of mb.

    100 concurrent clients
    1 megabyte response
    node.js-> 822 requests/sec
    nginx-> 708 requests/sec
    thin-> 85 requests/sec
    mongrel-> 4 requests/sec
    (bigger is better)

    What are you going to learn?

    • Advantages of node.js
    • Basic terminology.
    • Install node.js on windows, mac and linux.
    • Write simple programs.
    • Learn about NPM.
    • Installing modules.
    • Node.js express tutorial.
    • Node.js mongodb tutorial.
    • Node.js mysql tutorial.

    Advantages of node.js

    • Node.js uses java script and it is easy to learn.
    • Node.js runs on v8 Engine (a javascript interpreter: chrome uses it) which is very fast.
    • We can build highly scalable web applications using node.js
    • We can develop chat based applications, online games, API’s and many more.
    • Supports interaction with all major databases.
    • Node.js has 1000′s of third-party free modules.

    Companies depending on node.js

    ebay, Microsoft, Yahoo!, StrongLoop, OmniTI, Storify, local response, nodejitsu, Linkedin, Iris Couch, backbeam, recruitics, Transloadit, Uber, Voxer,when-to-manage, Jaleoo, Cloud9 IDE, NODE the FIRM etc..

    Basic terminology

    Asynchronous- Asynchronous means an event which is happening independently of other events. The main advantage of the asynchronous approach is scalability.
    Npm- Stands for node package manager. This will help us install the latest modules directly from the servers instead of checking for the latest version, downloading and copying to the appropriate directory. It also has other uses.
    Express- Express is a highly recommended web application framework for node.js. It takes care of the low level services for us.
    Module- A module is a independent, reusable piece of code. you can download them and use them directly in your project. ex: express, http, https etc are some of the modules for node.js.
    REPL-Read-Eval-Print-Loop provides a way to interact with javascript it can be used for debugging, testing. To try repl go to terminal(cmd prompt) and type node and try ex: console.log(“learn nodejs”);
    Mongoose- Mongoose is a MongoDB object modeling tool for node.js. It becomes easy to interact with mongodb database server using Mongoose module.
    Db-mysql- It is a node.js module to interact with mysql database.

    Installing Node.js

    Installation on windows

    • goto->nodejs.org->click on Install button->that will download a node windows installer.
    • install it just like any other application.
    • restart the computer.
    • open cmd prompt->type in node –version->it will show the node.js version number.
    • you can also check the npm version using npm –version command.

    Installation on Mac

    • installing node.js on mac as same as for windows.
    • you can also use brew to install node.js->brew install node
    • or use macports->port install nodejs

    Installation on linux

    • goto->shell
    • sude -i
    • $sudo apt-get install libssl-dev (optional installs openssl)
    • wget http://nodejs.org/dist/v0.10.10/node-v0.10.10.tar.gz
    • tar -vfx node-v0.10.9.tar.gz
    • cd node-v0.10.9
    • ./configure
    • make
    • make install(node is installed)

    If you have any problems during linux installation please make sure you have python and openssl installed.

    Try this: node.js example 1

    Create a file named nodeexample1.js
    Add the following code in it and save.

    var http = require('http');
    http.createServer(function (request, response) {
    response.writeHead(200, {'Content-Type': 'text/plain'});
    response.end('First node.js example');
    console.log('Server running at http://localhost:8123/');

    ->goto->cmd prompt->enter the directory where u saved the file->type in node nodeexample.js(u ll get: server running at
    ->then open browser and type->http://localhost:8124
    ->output-> First node.js example
    ->use ctrl+c to stop the server.

    Explanation of example1

    line1->when ever we are using any modules(builtin or external), we have to include them first.
    line2->http.createServer will return a new web server object. we have to create a server object to use http server.
    line3->sends response to the browser. You can also use response.write() method. these must be used only before response.end() method.
    line4->listens to the port. You can send an optional parameter of ‘localhost’.
    line5->console.log() will output to the console and not the browser window.
    Note- Whenever you modify the file, don’t forget to restart the server ie., stop the server using ctrl+c and run the file again.

    Try this: node.js example 2

    We will have a look at how to load static files
    create a file named nodejsexample2.js and copy the following code in it.

    var http = require('http');
    var fs = require('fs');
    http.createServer(function (request, response) {
    fs.readFile('index.html','utf8', function (err, data) {
    if (err){
    response.write('unable to load the requested file');}
    response.writeHead(200, {'Content-Type': 'text/html'});
    console.log('Server running at http://localhost:8123/');

    ->now run the file to start the server.
    ->the output will be the contents of the file.


    line2-> We have included filesystem module. Using this module we will be able to access the file system.This is builtin module no need to download it.
    line4-> readFile() function is used to read the files from the disk. function(err,data) is the callback function and it is mandatory.what happens here is node.js will not wait for the response and will carry on with the execution and when the response is ready it will be outputted.
    line5-> If there is an error reading the file there will be one response. If everythings ok then else part is executed. here ‘data’ stores the contents of your file we can change the name, and make sure that index.html exists in you current directory.

    We will have a look at node.js express framework in the next section.

    Acharya Vaddey is a Passionate Webmaster. He frequently writes articles on topics of SEO, SEM, SMM, Digital Marketing, Web Design/Development, Web/Internet Technologies.