What is Node.js?

What is Node.js?

Node.js is a JavaScript runtime environment built on Chrome’s V8 engine. It was originally created by Ryan Dahl, an American Software

Node.js is a backend JavaScript runtime environment that runs on the V8 engine and executes code outside of a web browser. Node.js interprets and executes a JavaScript-based script on its runtime environment. Node.js is one of the fastest server-side application platforms.

“You can never understand everything. But you should push yourself to understand the system.” — Ryan Dahl, creator of Node.js

  1. Node.js is open-sourced and thus has a vast ecosystem of many open-source libraries that can be leveraged using npm.
  2. Everything in Node.js is asynchronous.
  3. Node.js is single-threaded but supports concurrency with the help of events and callbacks.
  4. Node.js is cross-platform. It can be run on Windows, Linux, Unix, Mac OS X, etc.
  5. Node.js is based on one of the most popular programming languages — JavaScript.
  6. Node.js provides an easy way to build scalable enterprise applications.

Now, let's discuss some important theoretical concepts that will help you understand how Node.js works efficiently.

Node.js_Architecture_Workflow.png The Architecture Workflow of Node.js (Image Source — simplilearn.com)

Node.js is Asynchronous

All APIs in Node.js are Asynchronous and Non-Blocking. Non-Blocking is when the execution of additional JavaScript code in the Node.js process does not need to wait until a non-JavaScript operation completes. This essentially means that a Node.js server never waits for an API to return data. After calling the API, it will move on to the next API and use an event queue notification mechanism to keep track of the API request order.

As an example, let’s consider a case where each request to a web server takes 90ms to complete and 70ms of that 90ms is a database I/O operation that can be performed asynchronously. Since Node.js is asynchronous, it frees up that 70ms per request, to handle other external requests. This difference might seem small, but when added together over thousands of requests for a big application, Node.js & asynchronous programming can really improve efficiency and performance.

Node.js is single-threaded

Node.js uses a single-threaded model along with event looping for async processing. This improves performance and scalability. More processing, performance, and scalability can be achieved by doing async processing on a single thread under typical web request loads than the typical thread-based implementation.

Callback function in Node.js

Node.js due to its asynchronous nature doesn't wait for a particular function call to finish execution. Instead, it uses callback functions. All APIs written in Node.js make use of callbacks.

If a particular function, takes a large amount of time to execute, it would normally block the execution of the remaining program. However, Node.js uses callbacks and async programming to continue the execution of the remaining part of the program. These callback functions are executed once the original function execution is completed and do not affect the normal flow of the program.

In simple words, a callback function is a function that says, “once you’re done doing this, do this.”

Callback hell

Callback hell is a major issue that arises due to the usage of nested callbacks. It arises when each and every callback takes an argument as a result of the previous callback, which makes the code structure difficult to read and maintain. In addition to this, if there is an error in one function, all the corresponding callback functions get affected.

In order to avoid callback hell, promises are used. A Promise is a result of an asynchronous operation. In simple terms, it is an object or a piece of code that is returned, once a particular part of the code is executed. A promise has three states —pending, resolved, & rejected. Depending on whether there is an error in the code or not, a particular state of the promise is called. Thus, a promise chaining mechanism can be used to avoid callback hell that arises due to multiple nested callback functions.

callback_hell.jpeg Callback hell in JS (Source — medium.com/@jaybhoyar1997/avoiding-callback..)

Event loop in Node.js

The event loop is what handles asynchronous callbacks. It allows Node.js to perform non-blocking I/O operations — despite the fact that JavaScript is single-threaded — by offloading operations to the system kernel whenever possible. The event loop is an infinite loop that waits for tasks, executes the tasks once received, and then again waits for more tasks. The event loop executes tasks from the event queue only when the call stack is empty i.e. there is no ongoing task. Whenever a task is complete, the event loop fires an event that signals to the event listener function to execute. The event handlers decide what is to be done once a particular event has triggered.

Events may look similar to callback functions. However, the key difference between the two is that a callback function is executed whenever an asynchronous function call has been completed, and events are more like observers, that are waiting for the particular event to be triggered.

event_loop.jpg Source — tutorialspoint.com/nodejs/nodejs_event_loop..

NODE_ENV

NODE_ENV is an environment variable. During the execution of code, it can check the value of this environment and execute some additional lines of code. NODE_ENV is most popularly used to check whether the environment is in development or production so that some particular debugging lines of code can be executed or not. It basically acts like a flag that is used to check whether the code is being run in development or production, and based on that flag, the necessary code is executed.

Node Package Manager (npm)

Npm stands for Node Package manager. It is an online repository for publishing Node.js projects, and also a command-line utility that aids in installing packages that can be used in your Node.js application. An abundance of node packages are published on npm and many more are being added every day. You can search for the various packages here.

To summarize, npm is basically a package manager for node, that can be used to download various packages to be used in your application directly from your command line.

npm.png Node Package Manager (Source — en.wikipedia.org/wiki/Npm_(software))

Modules are nothing but JavaScript libraries that can be used to import some particular lines of code into a program, or to import a particular coding logic with simpler syntax, which further improves code readability. We can use the require() function with the module's name inside the parenthesis to import a particular module into your Node.js application.

  • HTTP

    The HTTP module creates an HTTP server that listens to server ports and gives a response back to the client. Syntax: const http = require(‘http’);
  • util

    This module provides access to some utility functions that can be quite useful for developers. Syntax: const util = require(‘util’);
  • fs

    This module includes events, classes, and methods that deal with file I/O operations. Syntax: const fs = require(‘fs’);
  • url

    This module has utilities for URL parsing and resolution. Syntax: const url = require(‘url’);
  • lodash

    Lodash is one of the most popular npm packages. It makes working with JavaScript easier by providing formats for working with arrays, numbers, objects & strings. Syntax: const lodash = require(‘lodash’);
  • underscore

    This is another very popular npm package. It is similar to lodash and provides formats for iterating & manipulating arrays, objects, strings, etc. Syntax: const _ = require(‘underscore’);
  • express

    Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. It makes API development using Node.js much easier. Node.js and Express are usually always used together for backend development. Syntax: const express = require(‘express’);

Package.json file

Every Node.js project contains a package.json file in their root folder that contains metadata about their project. It contains data about the various modules used in the project, their version numbers, the dependencies, dev dependencies, etc. It also contains data about the project such as the project description, the version, the licenses, etc. For example:

{
  "name" : "underscore",
  "description" : "JavaScript's functional programming helper library.",
  "homepage" : "http://documentcloud.github.com/underscore/",
  "keywords" : ["util", "functional", "server", "client", "browser"],
  "author" : "Jeremy Ashkenas <jeremy@documentcloud.org>",
  "contributors" : [],
  "dependencies" : [],
  "repository" : {"type": "git", "url": "git://github.com/documentcloud/underscore.git"},
  "main" : "underscore.js",
  "version" : "1.1.6"
}

Companies that are using Node.js

  • Amazon, Netflix, LinkedIn, eBay, PayPal, Trello, Uber, and Reddit use Node.js as their backend framework.
  • LinkedIn moved the back end of the mobile application from Ruby on Rails to Node. Two key reasons for this switch were efficient performance and scalability.
  • PayPal says swapping Java for Node.js on its servers is allowing it to serve web pages more rapidly and simplifying the creation of server-side software. PayPal started using Node.js as a prototyping platform but later moved to use it in production.
  • Uber needed a system to be reliable both to passengers and drivers. They named the 3 key reasons for choosing Node.JS - the ability to process big amounts of data quickly and reliably, convenient error analysis and quick code deployment, and ongoing improvement of the technology because of the open-source community.
  • Trello is among companies that use Node.js for its server-side and as a prototyping tool to build a single-page web application.

How do you create a simple server in Node.js that returns Hello World?

Now that you have learned the theory behind the working of Node.js, let’s get our hands dirty and write a simple “Hello World” Node.js program.

  1. Import the HTTP module
  2. Use createServer function with a callback function using request and response as parameters.
  3. Type “hello world.”
  4. Set the server to listen to port 3000 and assign an IP address
const http = require('http');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World!');
}).listen(3000);

Congratulations! You just wrote your first Hello World Node.js program!

nodejs.png Node.js Logo

Conclusion

I hope you have gained some knowledge on Node.js and now know why it has gained so much popularity. If you liked this article and would like to read similar work, be sure to follow me on Twitter where I post updates about publishing my latest articles. Please feel free to share your thoughts on this article in the comments section. If you guys have any suggestions regarding this article or on anything related to Node.js, I would love to know.