Node.js

Node.js logo. Source: Node 2018a.
Node.js logo. Source: Node 2018a.

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?
    How Node.js works. Source: Hamedani 2018.

    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 development focus, 2018 survey. Source: Node 2018b.
    Node.js development focus, 2018 survey. Source: Node 2018b.

    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?
    Event loop hands over slow tasks to worker pool and executes callbacks. Source: Gopi 2016.
    Event loop hands over slow tasks to worker pool and executes callbacks. Source: Gopi 2016.

    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 consists of phases. Source: Khan 2017.
    Event loop consists of phases. Source: Khan 2017.

    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.

Sample Code

  • // Example Node.js server code
    // Source: https://codeburst.io/node-js-by-example-part-1-668376cd4f96
    const http = require('http');
     
    const hostname = '127.0.0.1';
    const port = 3000;
     
    const server = http.createServer((req, res) => {
      res.statusCode = 200;
      res.setHeader('Content-Type', 'text/plain');
      res.end('Hello World\n');
    });
     
    server.listen(port, hostname, () => {
      console.log(`Server running at http://${hostname}:${port}/`);
    });
     

References

  1. Agarwal, Rachit. 2014. "Why use ExpressJS over NodeJS for Server-Side Development?" Blog, Algoworks, August 22. Accessed 2018-10-27.
  2. Capan, Tomislav. 2013. "Why The Hell Would I Use Node.js? A Case-by-Case Tutorial." Toptal. Accessed 2018-10-27.
  3. Chauhan, Shailendra. 2015. "Brief history of node.js and io.js." DotNetTricks, December 31. Accessed 2018-10-27.
  4. Chrzanowska, Natalia. 2017. "6 Types of Applications You Can Build With Node.js." Netguru, September 14. Accessed 2018-10-27.
  5. Collina, Matteo and Jinwoo Lee. 2018. "Node.js can HTTP/2 push!" Node.js Collection, on Medium, April 12. Accessed 2018-10-27.
  6. Gopi, Arun. 2016. "MEAN Stack – A Getting Started Guide." Reflections, May 04. Accessed 2018-10-27.
  7. Hamedani, Mosh. 2018. "How Node.js Works." Programming with Mosh, YouTube, January 25. Accessed 2018-10-27.
  8. JSConf. 2009. "The Speakers of 2009." The European JavaScript Conference, JSConf Berlin, November 7 & 8. Accessed 2018-10-27.
  9. Jaiswal, Abhishek. 2014. "Client-Side vs Server-Side Programming Languages." C# Corner, February 25. Accessed 2018-10-27.
  10. Joyent. 2015. "Joyent Moves to Establish Node.js Foundation." Press Room, Joyent, February 10. Accessed 2018-10-27.
  11. Khan, Daniel. 2017. "What you should know to really understand the Node.js Event Loop." Node.js Collection, July 20. Accessed 2018-10-27.
  12. Langer, Mike. 2017. "Common Uses for Node.js." DZone, October 13. Accessed 2018-10-27.
  13. Mardan, Azat. 2018. "Node Frameworks." Accessed 2018-10-27.
  14. Maso, Rolando Santamaria. 2018. "NO, you most probably don’t need Express in your Node.js REST API." Medium, June 10. Accessed 2018-10-27.
  15. McKenzie, Sebastian, Christoph Nakazawa, and Jamie Kyle. 2016. "Yarn: A new package manager for JavaScript." Facebook, October 11. Accessed 2018-10-27.
  16. NPM. 2018. "Homepage." NPM. Accessed 2018-10-27.
  17. Nemeth, Gergely. 2018. "History of Node.js on a Timeline." Blog, RisingStack, March 21. Accessed 2018-10-27.
  18. Node. 2018a. "Resources." Accessed 2018-10-29.
  19. Node. 2018b. "2018 Node.js User Survey Report." Accessed 2018-10-27.
  20. Node Blog. 2015. "Node v4.2.0 (LTS)." October 12. Accessed 2018-10-27.
  21. Node Docs. 2018. "Don't Block the Event Loop (or the Worker Pool)." Guides, October 26. Accessed 2018-10-27.
  22. Node GitHub. 2015. "2015-01-14 io.js v1.0.0 Release." January 14. Accessed 2018-10-27.
  23. Node GitHub. 2018. "node-v0.x-archive: Releases." Accessed 2018-10-27.
  24. Node.js Foundation. 2018. "About The Node.js Foundation." Accessed 2018-10-27.
  25. Orsini, Lauren. 2013. "What You Need To Know About Node.js." ReadWrite, November 07. Accessed 2018-10-27.
  26. Pal, Prince. 2016. "12 Benefits of Using Node.js For Web Applications." Think 360 Solutions, November 21. Accessed 2018-10-27.
  27. Pluszczewska, Bianka. 2016. "9 Famous Apps Built with Node.js." Blog, Brainhub, May 30. Updated 2018-08-30. Accessed 2018-10-27.
  28. Prasad, Kiran. 2017. "10 best Node.js app examples." ThinkMobiles, May 10. Accessed 2018-10-27.
  29. Schlueter, Isaac. 2009. "Preview: npm, the node package manager." nodejs, Google Groups, October 01. Accessed 2018-10-27.
  30. Seth, Gaurav. 2016. "Submitting a Pull Request to Node.js with ChakraCore." Windows Blog, Microsoft, January 19. Accessed 2018-10-27.
  31. Tech Insider. 1995. "Netscape and Sun Announce JavaScript, the Open, Cross-Platform Object Scripting Language for Enterprise Networks and the Internet." December 05. Accessed 2018-10-27.
  32. Williams, Owen. 2015. "Node.js and io.js are settling their differences, merging back together." The Next Web, June 16. Accessed 2018-10-27.
  33. Worker Threads Pool GitHub. 2018. "watson/worker-threads-pool." GitHub, October 15. Accessed 2018-10-27.
  34. libuv GitHub. 2018. "libuv/libuv." GitHub, October 23. Accessed 2018-10-27.

Further Reading

  1. Posa, Rambabu. 2015. "Node JS Architecture – Single Threaded Event Loop." JournalDev, April 08. Updated 2016-07-21. Accessed 2018-10-29.
  2. Kadlecsik, Tamas. 2016. "Understanding the Node.js Event Loop." Blog, RisingStack, November 01. Accessed 2018-10-29.
  3. Pluszczewska, Bianka. 2016. "9 Famous Apps Built with Node.js." Blog, Brainhub, May 30. Updated 2018-08-30. Accessed 2018-10-27.
  4. Node Docs. 2018. "Don't Block the Event Loop (or the Worker Pool)." Guides, October 26. Accessed 2018-10-27.

Article Stats

Author-wise Stats for Article Edits

Author
No. of Edits
No. of Chats
DevCoins
2
0
1845
2
0
53
1783
Words
11
Likes
11K
Hits

Cite As

Devopedia. 2018. "Node.js." Version 4, October 29. Accessed 2024-06-25. https://devopedia.org/node-js
Contributed by
2 authors


Last updated on
2018-10-29 07:52:06

Improve this article
  • MEAN Stack
  • Node Package Manager
  • Yarn
  • Socket.IO
  • Asynchronous Programming
  • Node.js Frameworks

Article Warnings

  • In Further Reading, replace these sub-standard sources: journaldev.com