# Node.js

## Summary


Since its birth in 1995, JavaScript has established itself as the de facto programming language on the client side of a web application. It executes within a web browser and thereby delivers more dynamic content and makes user experience more interactive. However, to execute code on the server side, we had PHP, ASP, Perl, Python, etc. Until Node.js, there was no popular JavaScript support on the server side.

Node.js is a JavaScript runtime that can be executed on servers, that is, outside a web browser environment. By default, it's based on Google's V8 JavaScript engine. It's open source and cross platform. It's steered by the Node.js Foundation.

Node.js adopts an event-driven architecture and asynchronous I/O. This enables it to scale better, particularly for real-time web apps or apps that do a lot of I/O. The Node.js ecosystem includes thousands of packages and frameworks.

## Discussion

### What's the context for the creation of Node.js?

The web was invented based on the client-server model. Clients (such as web browsers) request the server for a resource. The server serves the content as a response. In this model, the server cannot initiate a response. Even if data has changed on the server, it has to wait for the client to give a request. This was alright when users were passive consumers of information. 

When social sites such as Facebook and Twitter arrived, everyone became a potential producer of information. There was a rise of chat apps, multi-user gaming and apps for real-time collaboration. We needed a method for servers to push data to clients. While Flash and Java Applets enabled this, they were isolated environments that used non-standard ports. 

Secondly, typical web servers launch a new thread to handle every client request. This means that scalability is limited by memory and the penalty of switching across threads. 

Node.js solves these problems by using single-threaded event-driven processing. It offloads I/O tasks to worker threads and processes them asynchronously via callbacks. Ultimately, this makes it easier for us to develop scalable real-time apps. 


### What sort of applications can benefit from Node.js?

Node.js is suited for apps that have lots of database accesses or waits on network connections. When there's a need to scale your app to thousands of parallel requests, Node.js does this well without hitting performance limits. On the flip side, Node.js is not suited for CPU-intensive tasks, which will block the main thread. 

With Node.js, stream processing of data is possible. Worker threads do this processing. It's also good for queueing inputs for later batch processing. 

Node.js with Socket.IO makes it easy to build real-time apps such as chat apps or games. It can be used as a proxy to handle many simultaneous connections. Apps with rich real-time dashboards can be created. Microservices architecture can be implemented with Node.js. 

If your codebase spans both client and server concerns, Node.js helps you to write in a single language, JavaScript. This makes it easier to later refactor the code if necessary. JavaScript is a natural fit for transferring JSON data or working with object databases such as MongoDB without the extra processing of encoding or decoding. 


### How does Node.js work under the hood?

When a Node app starts, callbacks are registered for events. A **callback** is nothing more than executable code that must be called when the event occurs. After this initialization phase, Node enter the **Event Loop**. In this loop, Node waits for events to happen and trigger the appropriate callbacks. It's for this reason Node.js is said to use **event-driven programming** paradigm. 

For example, a client requests a web page. Event loop will invoke the associated callback. This is **synchronous**: event loop will wait for the callback to complete. However, if the callback requires some data from a database, the event loop being a single thread will get **blocked** since I/O is slower compared to CPU. Other events will start queueing up. 

To prevent this, Node.js adopts **non-blocking asynchronous I/O**: slow tasks will be handed over to the **Worker Pool**. This frees the event loop to move on the next event. When a worker completes its task, it will signal an event, at which point, the event loop will process its callback synchronously. 


### Could you share more details on the event loop?

Event loop is also called main loop, main thread, or event thread. It's where code executes synchronously, that is, the event loop will wait until execution completes. If there are asynchronous calls to be made, event loop will register their callbacks and handover the tasks to worker pool. The idea is to perform only lightweight tasks on the event loop so that it can handle as many requests as possible. Throughput (requests per second) is an important concern for the event loop. 

It's important to note that the application and the event loop run within a single thread. All events are processed in the same thread. Pending events are not stored in a single queue. Rather, the event loop is a set of phases, with each phase having events queued for processing. 


### Could you share more details on workers?

Worker Pool manages a number of threads that can be used for both I/O-intensive or CPU-intensive tasks. The worker pool maintains a queue of tasks to be processed. When a worker becomes available, it's assigned a task. 

Both event loop and worker threads make use of `libuv`, a multi-platform support library for asynchronous I/O. It's implemented in C. Since operating systems often provide asynchronous interfaces, libuv will use these in preference to its own thread pool. 

Since Node v10.5.0, on an experimental basis, it's been possible to create pools, specify the number of workers or have control of one worker communicating with another. This is part of the `worker-threads-pool` package. 


### For my next web app, should I use Node.js or Express.js?

Express is a web application framework built on top of Node. You can build a web app with either of these but development will be faster when built with Express. Even better is to adopt the **MEAN** stack. This is a technology stack that combines MongoDB, Express, Angular and Node. 

Having said this, developers have a choice. Express is not the only Node.js framework. Others include hapi, koa or Sails. For fullstack, Meteor, Feathers or Keystone are alternatives to MEAN. 

What if you're building a REST API? In this case, performance is important. Express may be too heavy and slow for building REST API endpoints or an API gateway. You could use Node directly but there are Node frameworks that are minimalistic and optimized for REST APIs: fastify, restana, restify, koa, polka. 


### What are some success stories with Node.js adoption?

Paypal built a Node.js app in half the time with 33% fewer lines of code compared to its Java codebase. LinkedIn's mobile app using Node.js backend is 20x faster and uses only 3 servers compared to 30 that Ruby on Rails needed. At Netflix, Node.js has reduced app startup time by 70%. Groupon web pages are 50% faster with Node.js compared to Rails. They can also serve much higher traffic. At GoDaddy, they now require 10x fewer servers and time-to-first-byte is down to 12ms from 60ms. 

Uber is able to process 2 million remote procedure calls per second, thanks to Node.js. Medium is now able to deploy faster. At NASA, by moving to a single database and Node.js they reduced access times by 300%. 


### What are some useful resources for a Node.js developer?

Guides and official documentation are available from the main Node.js site. For latest news, you can follow the Node.js Foundation website. The Foundation's site includes resources and case studies.

Among the package managers for Node.js are npm and yarn. As of October 2018, the npm site hosts about 800,000 JavaScript packages. Node Frameworks gives a list of frameworks to consider. To install and manage multiple versions of Node, use Node Version Manager (NVM).

A video series by Net Ninja titled Node JS Tutorial for Beginners is a good place to start for beginners.

A curated list of Node.js resources is available from Angular Minds.

## Milestones

Dec  
1995

Netscape Communications and Sun Microsystems announce JavaScript to the world. This includes **Netscape LiveWire** that allows server-side execution of JavaScript. Hence, Node.js is certainly not the first to enable server-side JS execution.

2009

Ryan Dahl and others at Joyent develop Node.js with initial support for only Linux. The name *Node* is coined in March. Being open source, version 0.1.0 is released on GitHub in May. In November, Ryan Dahl presents Node.js at JSConf in Berlin. 

2010

As a web development framework based on Node.js, **Express.js** is released. For real-time event-based communication, **Socket.IO** is released. 

2011

To manage packages for Node.js, **Node Package Manager (NPM)** is released. An early preview of NPM can be traced to 2009, by Isaac Z. Schlueter. 

Dec  
2014

Node.js community undergoes a split, with the fork named **io.js**. This is because some feel that Node.js updates are slow, and even after five years the library is still in version 0.x. Some also note that Node.js is controlled by Joyent and it's less open than desired. In January 2015, v1.0.0 of io.js is released. 

Feb  
2015

Joyent, IBM, Microsoft, PayPal, Fidelity, SAP and The Linux Foundation come together to establish **Node.js Foundation**. The Foundation will see that Node.js is managed in a more open manner and have regular releases. In June, Linux Foundation announces that Node.js and io.js will merge their codebases. 

Sep  
2015

Version 4.0 is released. This is a combined release of both Node.js and io.js. It includes many ES6 features. Thus, the much awaited version 1.x of Node.js does not happen. Version 4.0 is so numbered probably because the last version of io.js is v3.3.1. 

Oct  
2015

Node.js v4.2.0 "Argon" is released as the first Long Term Support (LTS) release. This means that it will be supported for 30 months. 

Jan  
2016

Microsoft releases and open sources **ChakraCore**, a JavaScript engine for Node.js. This is therefore an alternative to V8 engine that's been the main runtime since the birth of Node.js. 

Oct  
2016

Developers at Facebook release **Yarn** as an alternative to npm. 

2017

Node.js is seen to have reached mainstream adoption. Online instances reach 8.8 million with 3 billion npm packages downloaded per week. Node.js is downloaded 25 million times this year, including a million in a single day. 

Apr  
2018

With Node.js version 9, HTTP/2 is now supported. This was available since July 2017 on an experimental basis.