articles

Home / DeveloperSection / Articles / Introduction of Node.js

Introduction of Node.js

Anchal Kesharwani5908 18-Aug-2014

In this article, I am explaining the concept of nodejs.

Introduction

“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.” Beyond that, it’s worth noting that Ryan Dahl, the creator of Node.js, was aiming to create real-time websites with push capability, “inspired by applications like Gmail.

To start using Node.js, you must first understand the differences between Node.js and traditional server-side scripting environments (eg: PHP, Python, Ruby, etc). Node.js uses a module architecture to simplify the creation of complex applications.

Chances are good that you are familiar with asynchronous programming; it is, after all, the "A" in Ajax. Every function in Node.js is asynchronous. Therefore, everything that would normally block the thread is instead executed in the background. This is the most important thing to remember about Node.js. For example, if you are reading a file on the file system, you have to specify a callback function that is executed when the read operation has completed.

Node.js is only an environment - meaning that you have to do everything yourself. There is not a default HTTP server, or any server for that matter. This can be overwhelming for new users, but the payoff is a high performing web app. One script handles all communication with the clients. This considerably reduces the number of resources used by the application. For example, a node.js code for simple loop which iterate number of times and it consumes less time to other server side language as like PHP.

Modules

Node.js uses a module architecture to simplify the creation of complex applications. Modules are akin to libraries in C, or units in Pascal. Each module contains a set of functions related to the "subject" of the module. For example, the http module contains functions specific to HTTP. Node.js provides a few core modules out of the box to help you access files on the file system, create HTTP and TCP/UDP servers, and perform other useful functions.

Module are include by simply calling the require() function, like this:

var http = require('http');

The require() function returns the reference to the specified module. In the case of this code, a reference to the http module is stored in the http variable.

In the above code, we passed the name of a module to the require() function. This causes Node to search for a node_modules folder in our application's directory, and search for the http module in that folder. If Node does not find the node_modules folder (or the http module within it), it then looks through the global module cache. You can also specify an actual file by passing a relative or absolute path, like so:

var myModule = require('./mySampleModule.js');

The modules are encapsulated with pieces of code.  The code means it have functions and variables.

Global Scope 

Node is a JavaScript environment running in Google's V8 JavaScript engine. As such, we should follow the best practices that we use for client-side development. For example, we should avoid putting anything into the global scope. That, however, is not always possible. The global scope in Node is GLOBAL (as opposed to window in the browser), and you can easily create a global variable of function by omitting the var keyword, like this:

globalVariable = 1;

globalFunction = function () { ... };

Once again, globals should be avoided whenever possible. So be careful and remember to use var when declaring a variable.

Conclusion

In this blog, we describe the concept of node.js and modules, and scope.


Updated 29-Nov-2017

Leave Comment

Comments

Liked By