Why Node.js Is More Important Than You Think

Node.js

Node.js is an open-source, cross-platform JavaScript runtime environment for developing server-side web applications, that uses the V8 engine developed by Google for use in Chrome. Node.js applications are written in JavaScript and can be run within the Node.js runtime on OS X, Microsoft Windows and Linux. The V8 engine compiles JavaScript into native machine code which executes at lightning speeds.

High Performance Web Development

Node.js enables you to build fast, scalable web applications capable of handling a huge number of simultaneous connections with high throughput.

Node.js provides an event-driven architecture and a non-blocking I/O API designed to optimize an application's throughput and scalability for real-time web applications. 

 Event loop

In addition to lightning fast JavaScript execution, the real magic behind Node.js is the event loop. The event loop is a single thread that performs all I/O operations asynchronously. Traditionally, I/O operations either run synchronously (blocking) or asynchronously by spawning off parallel threads to perform the work. This old approach consumes lots of memory and can be the source of performance issues. In contrast, when a Node application needs to perform an I/O operation, it sends an asynchronous task to the event loop, along with a callback function, and then continues to execute the rest of its program. When the async operation completes, the event loop returns to the task to execute its callback.

Node.js registers itself with the operating system so that it is notified when a connection is made, and the operating system will issue a callback. Within the Node.js runtime, each connection is a small heap allocation. Traditionally, relatively heavyweight OS processes or threads handled each connection. Node.js uses the event loop for scalability, instead of processes or threads. In contrast to other event-driven servers, Node.js's event loop does not need to be called explicitly. Instead callbacks are defined, and the server automatically enters the event loop at the end of the callback definition. Node.js exits the event loop when there are no further callbacks to be performed.

 Asynchronous I/O

By performing all I/O intensive operations asynchronously, Node.js is able to scale to large volumes of clients. Node.js is built to handle asynchronous I/O from the ground up and is a good match for handling a lot of common web and network development tasks. 

Node.js can read/write streams to websockets just as well as it can read/write streams to HTTP. For example, we can pipe stdout from a running process on the server to a browser over a websocket, and have the webpage display the output in real-time.

In other words, reading and writing to network connections, reading/writing to the filesystem, and reading /writing to the database, all very common tasks in web applications, execute very, very fast in Node.


Node.js is used by many large companies including Ebay, IBM, Microsoft, Yahoo!, Walmart, Groupon, SAP, LinkedIn, PayPal and GoDaddy.

PayPal reported: By using Node for their account overview page, one of the most trafficked apps on the website, they were able to double the number of requests per-second and reduced response time by 35%.

Walmart is able to serve some very sophisticated features to mobile users by using Node.js. 

In 2013 WalMart Labs put all of their Mobile traffic through Node.js on black-friday, the busiest shopping period of the year. The team at WalMart Labs tweeted against #nodebf tag showing the performance of the Node.js application. On Black Friday the WalMart servers didn’t go over 1% CPU utilisation and the team did a deploy with 200,000,000 users online.

Because of the modular nature of Node, developing new services with Node.js is easy. Writing applications the Node-way means that applications are composed of small modules, which are piped together. This enables changes to be made to a module or new functionality to be added without requiring changes to be made deep inside the entire code-base. Over time traditional monolithic applications become rigid and difficult to adapt as new requirements are added. Eventually traditional applications begin to creak under the weight and the stress put on them by the requirements they were not designed for.


The package manager for Node.js is npm and it works very well. In many ways it resembles package managers from other ecosystems, but npm is fast, robust, and consistent. It does a great job specifying and installing project dependencies. It keeps packages isolated from other projects, avoiding version conflicts. But it also handles global installs of shell commands and platform-dependent binaries.


Creating an HTTP Server is trivial with Node.js

Node.js contains a built-in library to allow applications to act as a web server without software such as Apache HTTP Server, Nginx or IIS.

In contrast to other web development languages, we do not need to set up a seperate HTTP web server. With Node.js, when the application is implemented, the whole HTTP server is part of the implementation.

Below is the code for creating a basic Node.js HTTP server. In the root directory of your application, create a file named server.js and fill it with the code below:

var http = require("http");

var server = http.createServer(function(request, response) {

  response.writeHead(200, {"Content-Type": "text/html"});

  response.write("<!DOCTYPE html>");

  response.write("<html>");

  response.write("<head>");

  response.write("<title>Hello World Page</title>");

  response.write("</head>");

  response.write("<body>");

  response.write("<h1>Hello World!</h1>");

  response.write("</body>");

  response.write("</html>");

  response.end();

});

server.listen(9000);

console.log("Server is listening");

In a terminal window cd to the root directory of the application, to run the server.js script with Node.js type the command: node server.js

Now open your favorite browser and hit https://localhost:9000/. This should display a web page that says “Hello World!”. 

Well now, lets see what’s actually going on here.

The first line requires the http module that ships with Node.js and makes it accessible through the variable http.

You defined the requestHandler function to handle the requests.

You called one of the functions that http module offers: createServer. This function returns an object, which is stored in var server.

This(server) object has a method named listen, and takes a numeric value which indicates the port number our HTTP server is going to listen on.

Whenever a request received, requestHandler function gets triggered and two parameters are passed into it, request and response.

You write the page header with response.writeHead(), which sends an HTTP status and content-type in the HTTP response header.

You send the HTML and H1 wrapped text “Hello World" in the HTTP response body with response.write() .

You finish the response with response.end().

 
Contact us to learn more about getting a high performance Node.js application built for your website.  
Sharing is caring:

Dialogue & Discussion