Node.js development – an introduction to the popular JS runtime environment

Your complete guide to Node.js in the context of the JavaScript ecosystem - technical run-down, pros, cons, applications and more

Software developmentUPDATED ON June 23, 2021

Author

Node.js development

Node.js is a runtime environment that allows software developers to work on the frontend and backend of web applications using JavaScript. It can be used in full-stack JavaScript development and features APIs to support HTTP requests, file systems and other server-side features.

Node.js was written and introduced by Ryan Dahl in 2009 and is an open-source cross-platform JavaScript runtime environment for executing JavaScript code server-side, and uses the Chrome V8 JavaScript engine.

Node.js as part of the JS ecosystem for back-end web development

With Node.js, you can now ‘extend’ JS to design and develop code to work on the server-side. This means you move away from the DOM model, browser intrinsics and other JS framework components. With Node.js, you can design standalone JS-based applications and essentially work outside of the browser engines.

Node.js offers its own ecosystem of software modules and packages that you can use to build applications with ease.  It has a library of over 350 thousand packages. With a strong and resilient Node engine and Node community support, you can learn as you code with Node.js.

K&C - Creating Beautiful Technology Solutions For 20+ Years . Can We Be Your Competitive Edge?

Drop us a line to discuss your needs or next project

Before going into more detail on Node.js development as a back-end technology choice, let’s quickly cover some web development and JavaScript ecosystem fundamentals:

Synchronous Process

Legacy web applications are synchronous in nature. When you interact with the web user interface in the browser, the browser makes a request to the server. The server responds to the request with the output. This request and receive process is essentially a synchronous process.

Asynchronous Web

Today’s modern web is asynchronous. You can get updates as soon as the state of the system changes, without any interaction with the UI. Examples of asynchronous applications or sites include any that display changing dynamic data like a social media feed, stock portfolio, an auction site, purchase data, or a timer/calendar.

Javascript

Real-time updates to websites or web-based apps require client-side rendering. JavaScript, a lightweight interpreted programming language, is the programming language of choice for web applications that need to make use of client-side rendering or at the server-side. JavaScript is synchronous and single-threaded.

Full Stack Javascript

Many, if not most, dynamic, asynchronous web applications in 2021 are coded, both client-side and server-side, using only JavaScript. Full-stack JavaScript development refers to both client and server-side software using JavaScript.

Event-driven approach

A programming approach that uses events to trigger required functions. An event can be anything, such as typing a character or clicking a mouse button. The event on an element is associated with a call-back function. This function is executed whenever an event is triggered.

Front end (of a website)

The part of a website that the user interacts with directly. It is also referred to as the ‘client side’. The front end includes items that users experience directly: text fonts, colour and styles, images, graphs and tables, buttons, navigation menu and so on. The languages used to develop the front end of a website are HTML, CSS, and JavaScript.

Server-side (Backend)

Scripts are written and run on the server-side to produce dynamic web page content before the page is sent to the web browser. Server-side technologies execute on a machine in private – the client or the user usually only sees the result of the execution.

SPA

Single Page Client Applications. Gmail or Twitter are SPAs. All the content is downloaded upon initial load.

Node.js – a definition and key vocabulary

Node.js is an open-source server-side runtime environment built on Chrome’s V8 JavaScript engine. It provides an event-driven, non-blocking (asynchronous) I/O and cross-platform runtime environment for building highly scalable server-side applications using JavaScript.

Now let’s explain some of the terms that are part of understanding Node.js specifically.

I/O (input/output)

I/O or Input/Output refers largely to the interaction of the program with the system’s disk and network. Examples of I/O operations include reading or writing data from/to a disk, making HTTP requests, and interacting with databases. I/O operations are slower compared to accessing memory (RAM) or doing work on the CPU.

Synchronous vs Asynchronous

Synchronous execution refers to code that executes in sequence in which it is written. The program is executed one line at a time. When a function is called, the execution halts until the function returns or closes, before continuing to the next line of code.

Asynchronous (or async) execution refers to execution that does not run in the sequence it appears in the code. In async programming,  the program does not wait for the task to complete and can move on to the next task. An async operation is generally I/O related.  Generally, computation-related tasks are synchronous. Tasks related to I/O or timing are asynchronous. I/O operations are done asynchronously since they are slow and will block further execution of code.

Blocking vs Non-blocking

Blocking refers to operations that block further execution until the operation finishes while non-blocking refers to code that does not block execution.

Node.js documentation states that blocking occurs when the execution of additional JavaScript in the Node.js process must wait until a non-JavaScript operation completes. Blocking methods are synchronous and non-blocking methods run asynchronously.

JavaScript runtime environment

Node.js is a JavaScript runtime environment. The Chrome browser JavaScript and Node.js run on the V8 JavaScript runtime engine. This engine converts JavaScript code to native machine code directly. Machine code is low-level code that the computer can run without the need for an interpreter. This improves the speed and performance of the JS code.

You must understand the concepts of the Node runtime environment before you get started with Node.js. This article briefly touches upon some aspects of the node.js environment. But you must be hands-on with Node.js packages and uses as you get into core Node programming.

Cross-Platform

Node.js allows you to use JavaScript on the front-end, the middle-ware, and the back-end and on any OS, including MacOS, Linux, and Windows, making it cross-platform.

For example, a Node.js developer can create a cross-platform desktop application for Windows, Linux, and Mac. You can also create apps for mobile, desktop, and web development.

Node.js is part of some very popular web development stacks, such as the MERN stack, MEVN stack, and MEAN stack.

JavaScript runs on all levels of an application in the MEAN stack, allowing you to build the whole application from scratch using JS as the only programming language.

Scalability

Scalability is how able an application’s architecture is to handle growth in terms of user numbers and the general resources demand placed on the code and server. A good web application must seamlessly and efficiently handle growth, and be designed with scalability in mind. You don’t want to have to completely redevelop an application when it hits a certain level of popularity”

A scalable web application must handle an increase in users and load, without disrupting the user experience. To build a scalable web application, you must consider factors like overall user experience, the potential for a surge in users, page loading times, the time required to change and test code, cost of updating the entire app, ease of maintenance, time to recover from an application crash, restart with zero downtime and so on.

Node.js has built-in scalability features. It is designed to be scalable. Node has modules that allow you to recover or restart an application with zero downtime and improve the Node processes’ availability should it hit a problem.

Some of the common methods that Node.js offers to scale an application are:

  • Cloning using the Cluster module
  • Decomposing using microservices
  • Splitting or data partitioning

Key Takeaways

Now that we have touched upon some basic concepts, let’s revisit the definition of Node.js

Node.js is an open-source server-side runtime environment built on Chrome’s V8 JavaScript engine. It provides an event driven, non-blocking (asynchronous) I/O and cross-platform runtime environment for building highly scalable server-side applications using JavaScript.

The key takeaways are:

  • Node.js is server-side. That is, you write code for server-side (back-end), but it is also for JS full-stack – frontend, middleware and backend.
  • Node.js is open source and you can exploit the open source advantages and the online community support to fast track your learning.
  • Node.js is a runtime environment, with an ecosystem of thousands of libraries of its own. It is not just a library or a framework. It is a runtime env where you can work across the stack to develop and test code, run applications.
  • It runs off the Chrome V8 JS engine, which is robust.
  • It is cross-platform – can be used on all types of OS and devices.
  • Allows I/O and non-blocking operations. It provides for scalability so that you can build applications that are high performance, accept high user loads and have zero downtimes during recovery.

Other JS engines and Server-side technologies

As mentioned earlier, Chrome uses Node.js. Some of the other browsers with their own JavaScript engines are:

All the engines implement the ECMA ES-262 standard, also called ECMAScript, the standard used by JavaScript.

Some of the server-side technologies are Apache HTTPD, Tomcat, NGINX, Microsoft IIS (web servers), PHP, Python, Ruby, Java, ASP, C (languages), SQL Server and MySQL (databases).

NodeJS Uses

Node.js is perfect for data-intensive applications as it uses an asynchronous, event-driven model. You can use I/O intensive web applications like video streaming sites. You can also use it for developing: Real-time web applications, Network applications, General-purpose applications, and Distributed systems.

Node.js has a unique advantage because millions of frontend developers that write JavaScript for the browser are now able to write the server-side code in addition to the client-side code without the need to learn a completely different language.

This runtime environment is most suitable for developing real-time apps that process large numbers of online requests on the client-side that don’t require advanced security, with top-notch user experience and prompt response time. This includes live chats, instant messengers, online games, video conferencing and e-commerce services, and collaborative platforms for multiple users (Google Docs, Trello, Dropbox, etc.)

  • It is generally fast
  • It rarely blocks
  • It offers a unified programming language and data type
  • Everything is asynchronous
  • It yields great concurrency

Node.js development basics

Node.js is called Node because it is used to build simple single-process blocks called nodes. The nodes can communicate with each other with good networking protocols and are scalable which helps to build large distributed applications.

I/O operations in Node are carried out asynchronously, with the lines of code adhering to a non-blocking pattern. Node.js, as a package, contains an interpreter and a compiler.

As you begin to use and learn node.js, ensure you are familiar with some of the following concepts and utilities.

Callbacks

A callback is a function that is passed as an argument to another function. The callback is invoked (called back) inside the outer function to complete at a convenient time. The invocation can be immediate (sync callback) or at a later time (async callback). The right way to handle several concurrent connections is to have a single thread, an event loop, and non-blocking I/Os. For example, when you query a database, you give a callback with the query. The program continues executing other lines of code. When the query results come back, you can execute the callback.

Package managers

Node package managers allow you to get the plugins, libraries, and dependencies through simple commands. Package managers find these entities in the repositories and install them to create a larger application stack. They also update these libraries and install new ones.

npm

npm is the default package manager for Node.js and it gets installed into the system when Node.js is installed. It can manage packages that are local dependencies and also global JavaScript tools.

ECMAScript features

To fully comprehend how to work with the node.js library, you must read the documentation on the following:

CLI

CLI is a command-line interface. Using the Node CLI, you can run nvm commands, nmp commands, and also run your Node.js application.

NVM

NVM stands for Node Version Manager. It’s useful when for multiple projects that are using different Node.js versions.

Database

Since Node.js is primarily for the back-end, you must know some relational and non-relational databases basics and how to connect with a database.

MongoDB is a common non-relational database used with Node.js. MongoDB is a NoSQL, cross-platform, document-oriented database. It provides high performance, high availability, and easy scalability.

HTTP queries

To maintain the production environment of the application, you must know tools like pm2 or forever, that allow you to restart the node.js app in case of a crash.

 Docker

Docker is a data virtualization tool that allows you to place a Node.js application into a container that can be run on any computer with a minimum setup. Just install Docker and run a container.

Docker compose

Docker-compose helps to manage docker containers with different parts of the app: node.js app handling, database running inside, client-side code, or a frontend code. You can easily set up all these parts.

Node.js Modules

Modules are like JavaScript libraries that can be used in a Node.js application to include a set of functions. To include a module in a Node.js application, use the require() function with the parentheses containing the module’s name.

HTTP – Includes methods, classes, and events to create a Node.js HTTP server.

util – Includes utility functions useful for developers.

fs – Deals with file I/O operations. Includes events, classes, and methods.

URL – Includes methods for URL parsing.

query string – Includes methods to work with query string.

stream – Includes methods to handle streaming data.

zlib – Includes methods to compress or decompress files

Node.js development pros

A unique advantage of Node.js is frontend developers familiar with writing JavaScript for the browser can now also write the server-side code in JS without having to learn a new language for server-side code.

  • Node.js is an open-source framework under MIT license. (MIT license is a free software license from the Massachusetts Institute of Technology (MIT).)
  • Uses JavaScript to build server-side applications.
  • Lightweight framework that includes bare minimum modules. Other modules can be included as per the need of an application.
  • Asynchronous by default. It is faster.
  • Cross-platform framework which can run on Windows, MAC, or Linux

Node.js development cons

Node.js is not a one-size-fits-all solution. Its efficiency decreases as the CPU load grows. Apps built with Node.js may fail to handle heavy-computing operations, blocking incoming requests and reducing response speed.

Applications based on Node.js

Here are the best examples of apps that fully embrace Node.js as part of a modern technology stack.

  • Streaming apps like Netflix
  • Social networking apps like Linkedin, Twitter
  • Data collection, visualisation and analysis apps
  • Fintech apps like Paypal
  • Online stores and marketplaces like eBay, Alibaba
  • Enterprise applications like Walmart
  • Collaboration tools like Trello
  • Real-time applications, such as project management and collaboration tools that run across different devices
  • E-learning platforms like Quizlet
  • IoT solutions

Some kinds of applications Node.js probably isn’t a good choice for

Node.js is not generally a good fit for applications that involve CPU-intensive operations like image processing or other heavy computation work because it takes time to process a request and thereby blocks the single thread.

Node.js Under the Hood

Node.js development environment can be set up in Windows, Mac, Linux and Solaris. Some of the tools or the SDK for developing a Node.js application are:

  • Node.js
  • Node Package Manager (NPM) is part of Node.js installation since version 0.6.0. You need not install separately.
  • IDE (Integrated Development Environment) or TextEditor

Node.js can be installed in different ways. The following post highlights the most common and convenient ones.

https://nodejs.dev/learn/how-to-install-nodejs

Some of the other tools you must be familiar with are:

  • Compilers like Babel
  • Module bundlers (Browserify, Rollup, Parcel)
  • Task runners (Gulp and Grunt)
  • Plugins

Node.js code example

To get started, you can go to free tutorials and run sample node.js applications to get a feel.

Here is a sample code that will print “My first Node.js trial!!”

Sample file: main.js

var http = require("http");
http.createServer(function (request, response) {
   // Send the HTTP header 
   // HTTP Status: 200 : OK
   // Content Type: text/plain
   response.writeHead(200, {'Content-Type': 'text/plain'});
   
   // Send the response body as "Hello World"
   response.end('My first Node.js trial!! \n');
}).listen(8081);

// Console will print the message
console.log('Server running at http://127.0.0.1:8081/');

To execute the code:

$ node main.js

Resources

https://nodejs.dev/learn

In summary

This article listed some key basic programming concepts you must know before you start server-side programming using Node.js. You must learn JS thoroughly, understand I/O operations, event loops and core programming concepts before you get started with Node.js.

When does IT Outsourcing work?

(And when doesn’t it?)

Related Service

Cloud Native Development, Migration, Infrastructure & Consulting Agency

Read more >

Kubernetes Consulting, Training, Support & Management

Read more >