Node.js is an open-source, cross-stage runtime environment for creating server-side web applications. Node.js was created in 2009 by Ryan Dahl and engineers worked in Joyent. It was initially released in 2009 supporting just Linux. It's advancement and maintenance was driven by Dahl and supported by Joyent.

Node.js allows the formation of web servers and networking tools utilizing JavaScript and a gathering of "modules" that handle different cor functionality. Modules handle file system I/O, networking, binary information, cryptography functions, data streams and other center functions.

Let's find out more


The modules of Node applications:

Callbacks:

The callbacks library consists of four universal functions:

Set timeout (callback, delay, [...]):

Schedule the execution of the given callback after postponement milliseconds. Gives back a timeout Id for conceivable use with clearTimeout(). Alternatively, you can also pass contentions to the callback.

Set Interval(callback, delay, [...]):

Schedule the rehashed execution of callback each deferral milliseconds. Gives back an interval Id for conceivable use with clearInterval(). Alternatively, you can also pass contentions to the callback.

ClearTimeout (timeout Id):

Prevents a timeout from activating.

ClearInterval (interval Id):

Stops an interval from activating.

These functions can be utilized to schedule callback for execution. The setTimeout function is helpful for performing housekeeping tasks, e.g. sparing the condition of the program to disk after a specific interval. Keep in mind that timeouts and intervals are just executed when the execution is gone back to the Node event circle, so timings are not inexorably accurate in the event that you have a long-running blocking task.

Events:

Event is a class which is utilized to give a predictable interface to activating and tying callbacks to events. It is utilized inside as a part of a large portion of the Node core libraries and gives a strong establishment to construct event based classes and applications.

To make a class which extends EventEmitter, you can use utils.inherit():

var EventEmitter = require(‘events’).EventEmitter;
var util = require(‘util’);

// create the class
var MyClass = function () { ... }

// augment the prototype using util.inherits
util.inherits(MyClass, EventEmitter);
MyClass.prototype.whatever = function() { ... }

EventEmitters allow you to include listeners - callbacks - to any subjectively named event (with the exception of New Listener, which is special in EventEmitter). You can attach various callbacks to a solitary event. To include a listener, use:

EventEmitter.on(event, listener) or EventEmitter.addListener(event, listener)

You can utilize Event Emitter. once(event, audience) to include a callback which will only be activated once, as opposed to each time the event happens. This is a decent practice, since you should keep the quantity of listeners to a minimum (indeed, on the off chance that you have more than 10 listeners, Event Emitter will warn you that you have to call emitter.setMaxListeners).

To trigger an event from your class, use EventEmitter.emit(event, [arg1], [arg2], [...]):

MyClass.prototype.whatever = function() {
this.emit(‘someevent’, ‘Hello’, ‘World’);
};

The transmit function takes a boundless number of arguments, and passes those on to the callback connected with the event. You can uproot event listeners utilizing EventEmitter.removeListener(event,) or EventEmitter.removeAllListeners(event), which evacuate either one listener, or all of listeners connected with a specific event.

Streams:

However, in most cases we just need to read or write through the data once, and in one bearing (forward). Streams are a deliberation over mostly buffered data get to that rearrange doing this sort of data preparing. Streams return littler parts of the data and trigger a callback when new data is accessible for handling.

The Node stream interface comprises of two sections: Readable streams and Writable streams.

Readable Streams:

Files fs.createReadStream(path, [options]): Returns a new ReadStream object.
HTTP (Server) http.ServerRequest: The request object passed while preparing the solicitation callback for HTTP servers.
HTTP (Client) http.ClientResponse: The request object passed while preparing the solicitation callback for HTTP client request.
Child process child.stdout: The stdout pipe for child processes dispatched from Node.js
Child process child.stderr: The stderr pipe for child processes dispatched from Node.js
Process process.stdin: A Readable Stream for stdin.
The stdin stream is stopped by default, so one must call process.stdin.resume() to read from it.

Writable Streams:

Event: ’drain’: After a write() method returned false, this event is transmitted to show that it is safe to write once more.

Event: ’error’: Emitted on error with the exception.

Streams are an optional method for getting to data from different sources, for example, the system (TCP/UDP), documents, child forms and user data. In doing I/O, Node offers us different alternatives for accessing the data:

Fully Buffered Access:

Completely buffered function calls like readFileSync() and readFile() uncover the data as one major blob. That is, perusing is performed and afterward the full arrangement of data is returned either in synchronous or asynchronous style.

// Fully buffered access [100 Mb file]

[allocate 100 Mb buffer]
[read and return 100 Mb buffer]

Partially Buffered Access:

Partially Buffered access routines are different. They don't treat information data as a discrete event, but instead as a series of events which happen as the data is being read or written. They permit us to get to information as it is being perused from disk, network or other I/O.

// Streams (and partially buffered reads) [100 Mb file]

[allocate small buffer]
[read and return small buffer]
[repeat 1&2 until done]


When you're composing a new function, You should know how do you convey errors to the code that called your function? The most imperative thing to do is document what your function does, including what contentions it takes, what it returns, what errors can happen, and what those errors mean. In the event that you don't know what errors can happen or don't recognize what they mean, then your project can't be correct with the exception of unintentionally. So in case you're composing a new function, you need to tell your callers what errors can happen and what they mean.

There are three basic examples for a function to convey errors.


While Node has an inherent debugger, Node Inspector gives a pleasant graphical interface to debugging node programs. Node Inspector is a debugger interface for node.js utilizing the WebKit Web Inspector, the well known java script debugger. There is an exceptionally decent project that let you utilize the Chrome DevTools with a node module keeping in mind the end goal to have the capable troubleshooting choices web designers got inside Chrome.

Think on the capacity to set a breakpoint and later going into functions and analyze variables, objects on the fly. Everything you need to install is NodeJS and since nowadays, NodeJS is accompanying npm (=managing NodeJS modules) it's one line of typing after you have node so as to install this module: node-inspector.

Install with npm-

npm install -g node-inspector

To utilize node-inspector, enable debugging on the node you wish to troubleshoot. You can either begin node with a debug flag like:

node --debug program.js

Debugging

  1. Start the inspector. You can put $ node-inspector & in background
  2. Open your http link with /debug?port= in your webkit based browser.
  3. Check the javascript source from node.
  4. Select a script and set some breakpoints (far left line numbers) or essentially include a debugger bring in your code.
  5. At that point watch the marginally outdated screencasts.

Node-inspector works precisely like the web inspector in Safari and Chrome.


with Express and Socket.io

There are a several new fancy web applications out there that are utilizing real-time functionality. You can see samples of this used to great impact in Google Docs' multi-client editing, and the way all the online networking sites update you of what is happening in real-time.

We are building a helpdesk ticket system. Which works more or less the same way of chat applications.

To start with we have to make the project folder, add a server folder to that, and after that make the package.json document for our server module.

// Package.json
{
name: 'helpdesk-ticketer-server',
description: 'The server code for helpdesk ticketing system'
}

At that point we simply need to include our conditions utilizing NPM.

npm install --save express socket.io

Next we'll add an app.js document to the server directory, and make an essential express application.

var express = require(express);
var socketio = require('socket.io');
var app = express();

var io = socketio.listen(app.listen(3000));
io.sockets.on('connection', function (socket)
{
console.log('A user has connected');
});

Here we are telling both express, and socket.io to listen to asks for made at port 3000.

We're going to utilize ember-cli to set up the customer code, so in the root catalog of our project, run ember new customer and ember-cli will simply ahead and make the greater part of the important standard code for us. Once this is done, I need to uproot the greater part of the un-essential folders from the customer dictionary. In this application we will be utilizing the Pod Structure, as this seems to be, as I would see it, a superior approach to lay out your records. Next we'll include the socket io customer as a reliance utilizing bower and update our brocfile.js so it is imported into our seller record on build.

We'll make an Ember Service to handle the connection to socket.io and also discharging events. Thusly we can have a singleton object that holds the web socket we are joined with that endures for the life of the application, or until the client unequivocally disconnect. Make a document called helpdesk.js in the application, service or directory. Next we'll make a system on this service that handles submitted a ticket to the server by a socket.io event.

You have seen the connection logic some time recently, however we have also included a listener as socket.on() here we go in the same of the event that we indicated back on the customer. Any information that is gone into the event from the customer is additionally accessible here in the callback event. Inside this callback it might be a smart thought to control the information anyway you require. Imperatively, we are calling socket.broadcast.emit(), giving the event a name and passing the ticket once again into it. We have to handle this on our customer.

At the point when the controller is introduced we're setting up a listener with socket.on which is listening for the ticket-submitted event from the server, the callback will be passed the ticket which we are essentially adding to the tickets array, in this manner updating our perspective in real time. Run our application with ember server from the command line and explore to http://localhost:3000.

Submit a ticket using the structure and it will be added to the list of tickets immediately. Do this with two separate program windows open and you ought to see both browsers update in real time.

While this is a basic example, hopefully you have got some good thoughts in your mind now about how this technology can be utilized to enormously enhance the responsiveness of your own projects.