What is NodeJS? How it works?

Node.js is part runtime environment and part library for building network applications using server-side JavaScript. It uses Chrome’s JavaScript runtime engine to execute JS code directly without the need for the browser sandbox. By all accounts, it looks like that technology is now dead in the water. Does node.js have what it takes to get server-side JavaScript off the ground? To answer that question, this article will explain how to use node.js. Afterwards, you can decide for yourself whether or not it’s a technology that you could see yourself embracing.

 

What is Node.JS & why it is popular?

A lot of the confusion for beginners to Node is misunderstanding exactly what it is.

  • An important thing to realize is that Node is not a web server. By itself it doesn’t do anything.It doesn’t work like Apache.There is no config file where you point it to your HTML files
  • If you want it to be a HTTP server,you have to write an HTTP server by using its built in libraries.Node.JS is just another way to execute code in your computer. It is simply a JavaScript run-time

To start using Node.JS, you must first understand the difference between Node.JS and traditional server-side scripting environment.

  • Other scripting languages uses separate server like Apache or nginx to run the application,which is the thread and process based, which means if the process is waiting for the I/O, whole thread is blocked
  • Whereas Node.JS using the ‘HTTP’ module can run on a stand-alone web server. It is asynchronous, event driven I/O. Every nodes instance runs in a single thread, so it can handle more number of concurrent requests as compared to Apache

 

What makes Node.JS fast?

First, Node is powered by Google’s V8 JavaScript Engine. The thing running you JavaScript code is the exact same thing the Chrome browser uses to run JavaScript code
Now Question is why V8 JavaScript Engine?
It has unique speed compare to other JavaScript engines, it compiles JavaScript directly into native machine code, while other languages PHP & Ruby, Java all have to run through an interpreter every time when they are accessed. Node will run your code as though it’s native application. So it screams with speed

Second, How fastly Node handles connections

When 100 people connect at once, rather than having different threads, Node will loop over those connections and fire off any events your code should know about. If a connection is new it will tell you. If a connection has sent you data, it will tell you. If the connection isn’t doing anything, it will skip over it rather than taking up precision CPU time on it. Everything in Node is based on responding to these events. So we can see the result, the CPU stay focused on that one process and doesn’t have a bunch of threads for attention. There is no buffering in Node.JS application it simply output the data in chunks.

 

Advantages to Using Node.js

Node.js is supposed to be three things: easy to use, fast, and scalable. It’s easy in that a little code goes a long way and uses it’s own concurrency model that’s a lot simpler than traditional O/S threads. Much like client-side JS libraries, node.js does provide abstraction so that a lot of the boilerplate code is handled under the covers. That’s a great thing if you’re a novice to intermediate developer, not so good if you’re an expert and you want to get at some low level functionality. The scalability results from node.js’s better memory efficiency under high-loads and non-blocking execution model. Functions in node.js almost never perform I/O directly, so processes never block. That leaves the developer free from worrying about dead-locking processes and able to focus on the code at hand. That’s just another reason why beginner to intermediate programmers are able to use node.js to develop fast systems.

 

Here is a code snippet that illustrates how easy it is to write a simple server. It listens on port 8080 and responds to requests with a Hello message:

var http = require("http");
http.createServer(function(request, response) { 
 response.writeHead(200, {"Content-Type": "text/plain"}); 
 response.write("Hello from the Node.js server!"); 
 response.end();
}).listen(8080);
console.log('Server is listening to http://localhost/ on port 8080…');

To run the server

  1. Save the above code in a file called server.js
  2. Navigate to the folder to which you saved the server file
  3. Type “node server.js” at the command prompt:
    • E:\CodePortal\NodeJS>node server.js
    • Server is listening to http://localhost/ on port 8080…
  4. Open your favorite browser, enter “http://localhost:8080” in the address bar, and voila!

 

Conclusion

I have always been a fan of using the same language on both the client and server sides. As it stands, with the array of server-side technologies available, it can be a real chore to reach a decision on which to use. Not to imply that variety is a bad thing, but there are definitely advantages to working with a familiar language. For that reason, let’s hope that node.js catches on.