An Introduction To Node.js
Node.js is an open-source and cross-platform JavaScript runtime environment developed by Ryan Dahl in 2009. Built on Google Chrome’s V8 JavaScript engine, node.js allows developers to use JavaScript in the backend to create server-side applications.
Node.js uses a light-weight single-threaded event event loop architecture which runs applications in an asynchronous, event-driven, non-blocking I/O model. This makes it perfect for developing highly scalable data-intensive real-time applications capable of handling multiple concurrent clients. However, because of its single-threaded architecture, node.js is not suitable for CPU-intensive applications.
Installing Node.js
In this post, we will focus on installing Node.js on a system running Ubuntu.
Node.js is available in the Ubuntu repository and we can easily install it using the apt
package manager.
First, refresh your local package index by running:
$ sudo apt update
Then, install Node.js:
$ sudo apt install nodejs
Check that the installation was successful by running node
or nodejs
in the terminal. It should start up a new node shell in a new line like this:
>
You can start coding right from the shell. Example:
> console.log("hello node!")
Output:
hello node!
You can also check the version by running node -v
or nodejs -v
.
It is recommended to also install Node Package Manager(NPM) with Node.js. NPM is an open source library of Node.js packages.
To install NPM, run the following command:
$ sudo apt install npm
Again, check that the installation was successful by running npm -v
or npm --version
.
Once installed, you can use NPM to installed Node.js packages like this:
$ npm install express
You can uninstall a node package using the npm uninstall command. Example:
$ npm uninstall express
To install both Node.js and NPM, for a specific version of node, we can use a Node.js PPA (personal packaging archive) maintained by NodeSource.
We can use curl
to add the particular Node.js PPA to the system like so:
$ curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
If curl
is not installed on your system, first install it using:
$ sudo apt install curl
Now, you can install the Node.js package using the following command:
$ sudo apt install nodejs
The nodejs package contains both the node and npm binaries. Verify that node and npm are installed using the commands node -v
and npm -v
.
Another very important way to install a specific version of node.js, is using the Node Version Manager (NVM).
Visit the NVM GitHub repository, and from the section page and copy either the curl
or wget
command to download and install the nvm script:
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.36.0/install.sh | bash
or
$ wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.36.0/install.sh | bash
(Note: Do not run these scripts with sudo
as it will enable nvm for the root user.)
Running the scripts will clone the project’s repository from Github to the ~/.nvm/
directory. You will have to close and reopen your terminal to start using nvm.
To check that nvm is installed, type nvm --version
. If you get a version number back like 0.36.0
, then you know nvm was successfully installed.
Now, you can install a specific version of Node.js, say, 14.5.0
by simply running:
$ nvm install 14.5.0
To install the latest LTS (long term support) version, use the command:
$ nvm install --lts
You can list the installed Node.js versions with the command nvm list
or nvm ls
.
If you want to change the currently active version, run nvm use
along with the version you want to switch to. Example:
$ nvm use 12.16.3
To change the default Node.js version, run the following command:
$ nvm alias default 14.5.0
That’s it. Now you have all that you need to start using Node.js to make backend applications.
Creating a basic HTTP server
Let’s create a basic server in Node.js. Create a file with the name server.js and add the following code in it:
const http = require('http');
const hostname = "127.0.0.1";
const port = 3000;
http.createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Node!\n');
}).listen(port, hostname, ()=>{
console.log(`Server is running at http://${hostname}:${port}/`);
});
Save the file, and from the terminal in the directory containing the file, run:
$ node server.js
You should see the output below:
Server is running at http://127.0.0.1:3000/
If we open the address in the browser, we should see the Hello Node!
message.
Now, let’s create the same basic web server, but using Express.
Basic Express Server in Node.js
Express is a web application framework for Node.js that allows you to spin up robust APIs and web servers in a much easier and cleaner way.
Simply install it via npm as you would with any other package.
$ npm install express --save
With express
installed, create a file with the name server.js and add the following code in it:
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello Express!');
});
app.listen(3000, () => console.log('Server is running at port 3000'));
If we open the address in the browser, this time we should see the message Hello Express!
.
Conclusion
That’s all I have for now. In this post, we covered quite a few concepts about Node.js, starting from installation using various methods, installing and removing packages, and usage in a sample web server application.
You can read more about Node.js from their official docs here.