Node.js is a runtime environment for the programming language JavaScript.
It allows to create server-side web applications in JavaScript.
Take into account that JavaScript was a programming language intended initially for the client side, so JavaScript back-end frameworks allows to add server side functionality without having to learn a different programming language.
To work with Node.js, you need to be familiar with JavaScript. You can read an introduction to JavaScript on this post.
Node.js is a runtime built on V8, the JavaScript Chrome engine.
Electron is a desktop application framework based on Node.js. You can read an introduction to Electron on this post.
Node.js Components
A Node.js project has at least the following components:
- package.json file: it contains the project metadata.
- scripts: they are JavaScript files
Installing Node.js
On Linux systems, you can install Node.js opening the terminal and typing:
sudo apt install nodejs
Node.js Tools
This section describes some Node.js tools.
npm
npm is the default package manager for Node.js, and it helps installing libraries for projects.
It is usually installed by default along with Node.js.
npm can be install on Linux systems opening the terminal and typing:
sudo apt install npm
NVS
Node Version Switcher (NVS) allows to work with different Node.js versions.
It is FOSS under an MIT license.
Node.js Modules
npm allows to install modules, that can also be considered packages or libraries.
Node.js has its own module system that coexists with the JavaScript module system, because the latter was developed later. These modules are not necessarily compatible.
CommonJS module is the official name for a Node.js module. These module files may have the .cjs file extension and/or have the type “commonjs” in the project’s package.json file. They are called with the require() function, and optionally using the import literal (though it is not the default method).
JavaScript modules were introduced in its version ES6. Because of this, ES6 module is a common way to refer to a module from the JavaScript native module system. These module files may have the .mjs file extension and/or have type “module” in the project’s package.json file. They are called using the import literal.
In case that the file extension for the module file is optional, but in case you do not use it you need to to specify the module type for the module in the package.json file.
This is a list of popular Node.js modules.
Node.js server frameworks
Web frameworks based on Node.js:
- Express.js
- Nestjs
- Fastify
Express.js
Express.js is a server framework for Node.js.
It is the de facto default server framework for Node.js, but it is not the only one.
Its package name is “express”.
Database Node.js packages
pg / pg-pool
pg / pg-pool adds PostgreSQL support.
mongoose
mongoose package adds MongoDB support.
Uncategorized Node.js packages
cors
Package for providing a Connect/Express middleware.
EJS
Embedded JavaScript (EJS) has the package name “ejs”.
EJS is a template engine that works well with Express.js.
EJS files uses the extension .ejs.
bodyparser
bodyparser is a body parsing middleware.
Node.js Concurrency
A basic tenet is that Node.js is asynchronous by default. It recognizes the complexity of multi-thread programming and avoids it by default, even for local operations.
The reason behind that is that Node.js was initially conceived as a web server that expects to receives hundreds of requests at the same time and using a thread for each one is not an optimized solution.
Nevertheless, Node.js also allows synchronous calls, but they are managed as exceptions.
Concurrency models in Node.js:
- Asynchronous single-threaded
- Worker thread
- Synchronous but non-blocking
- Synchronous and blocking
Asynchronous Single-threaded
As default, there is a single thread that manage all operations.
Asynchronous Node.js APIs are callback-based, as they were created before JavaScript had a Promise class.
Nevertheless, Promise-based variants of the callback-based Node.js APIs can be created using the util.promisify() wrapper.
There are also some predefined Promise-based functions in Node.js.
Worker Threads
Node 10 and later support Worker objects, which are a kind of threads.
They are used for CPU-intensive operations that requires exploiting multi-core processing rather than intensive I/O.
Inter-worker communication is done visa message passing and cannot easily share memory with each other.
Synchronous and non-blocking
An example of synchronous and non-blocking is the method readFile within the CommonJS module fs, fs.readFile()
.
Synchronous and blocking
Synchronous and blocking methods may be used during initialization, before concurrency issues may appear.
Some built-in Node.js functions have a synchronous variant.
An example of synchronous and blocking is the method readFileSync within the CommonJS module fs, fs.readFileSync()
.
Setting up a Node.js Project
Create a folder for the Node.js project. Place your .js program inside this folder.
Now you need to configure dependencies. To do so, the Node.js package manager npm is used.
Open a terminal, go to the proejct folder and type:
npm init
If it is a new project, it will ask some questions to configure the project (such as the project name and version). Based on this input, npm init
create a package.json file where it takes note of basic project data and all module dependencies within the code.
You can use npm to install a library inside a folder, going to that folder and entering a command like this:
npm install library_name --save
Alternatively, you can ask npm to install all dependencies referred in the package.json file just by typing:
npm install
Learning Node.js
You can find the official starting guide for Node.js on this external link.
Node.js examples
You can find the official JavaScript examples on this external link.
Node.js Documentation
You can find official Node.js documentation on this external link.