Search⌘ K
AI Features

Node.js Overview

Explore the core concepts of Node.js and understand its role as a JavaScript runtime for backend development. Learn about its built-in modules, cross-platform compatibility, single-threaded event loop, asynchronous programming, and debugging tools to set up a robust backend environment.

In this chapter, we focus entirely on the backend of our course management application. We do this by setting up Node.js, installing the needed packages, organizing the project structure, and connecting the backend and frontend. Before we go into the technical part of code writing, let’s take a brief overview of what Node.js is all about and its features.

What is Node.js?

Node.js was created about 12 years ago by Ryan Dahl. It’s a powerful open-source server-side platform built on Google Chrome’s JavaScript engine (V8 engine).

It enables JavaScript to run on the server. It can build various applications, such as real-time chat applications, web applications, data streaming applications, single-page applications, and much more. It’s worth noting that Node.js is not a programming language but a JavaScript runtime environment. The important parts of Node.js are noted below:

Built-in modules

Node.js has some built-in modules that don’t need to be installed inside an application through npm. We can use these built-in modules by referring to the require or import syntax:

const http = require('http');

Below is a list of some built-in modules of Node.js:

Built-in Modules in Node.js

Built-in Modules

Description

http

This helps create an HTTP server in Node.js.

https

This helps create an HTTPS server in Node.js.

buffer

This handles streams that contain binary data.

dns

This helps to connect to a DNS server and perform name resolution functions.

fs

This helps to handle the file system.

url

This helps to provide utility for parsing URLs.

path

This helps to provide methods that deal with handling file paths.

events

This helps process events.

stream

This handles streaming of data.

Cross-platform compatibility

Since Node.js applications are written with JavaScript, they can run on Windows, Linux, and OSX. This particular feature in Node.js makes the development process easier and faster because code can be used across different platforms, like web applications and desktop versions for Windows, macOS, and Linux.

Single-threaded model

In the case of Node.js, it uses a single-threaded model, similar to JavaScript. It uses event looping to handle multiple concurrent clients. This makes it easy to take many requests compared to traditional servers.

Asynchronicity

Synchronous programming is a situation where tasks are executed in sequence. It’s a traditional way of programming in which one task executes before the next task can run. On the other hand, asynchronous programming involves a process where tasks can run separately, and there is no issue of dependence. In the case of Node.js, all its APIs are asynchronous in nature. Once the server calls a specific API and doesn’t return any data, the server will move to the next available API before executing a process.

Debugger

Node.js has a built-in debugger that isn’t fully featured but can perform simple inspections. The debugger is used in the terminal by using the inspect argument before the name of the JavaScript file. For us to inspect a file called nodescript.js, we can run the command below:

$ node inspect nodescript.js

Global objects

Global objects are available in all modules in Node.js.
They’re objects that are part of JavaScript and can be used directly in the application without importing specific modules. Below is a list of some global objects in Node.js:

  • __dirname
  • __filename
  • Console
  • Process
  • setImmediate(callback[, arg][, ...])
  • setInterval(callback, delay[, arg][, ...])
  • setTimeout(callback, delay[, arg][, ...])
  • clearImmediate()
  • clearInterval()
  • clearTimeout()
  • queueMicrotask()
  • textDecoder
  • textEncoder