This article will cover the basic steps to start with Node.js. We will go step by step with basic concepts and implementation.
Topics covered in this article,
- Introduction to NodeJs
- Installation
- NPM (Node Package Manager)
- Simple example
Introduction to NodeJs
Node.js is an open-source and cross-platform JavaScript runtime environment. It is a popular tool for almost any kind of project. Node.js runs on various platforms like Windows, Linux, Unix, Mac OS X, etc. Node.js uses JavaScript on the server. Node.js uses asynchronous programming!
The main task that a server performs is to open a file on the server and return the content to the client. To understand how Node.js handles the requests, steps are as given below,
- Sends the operation to perform to the computer's file system.
- Ready to handle the next request.
- As the file system has opened and read the file, the server returns the content to the client.
In this way, Node.js eliminates waiting and continues with the next requests.
Node.js runs single-threaded, non-blocking, asynchronous programming, which is very memory efficient.
Comparing Node.js with ASP for handling the file request, ASP handle a request as,
- Sends the task to the computer's file system.
- Waits until the file system opens and reads the file.
- Returns the content to the client.
- Ready to handle the next request.
In this way the operations that are performed are synchronous but the tasks performed by Node.js are asynchronous
Some tasks that can be performed using Node.js,
- Node.js can create, open, read, write, delete, and close files on the server
- Node.js can add, delete, modify data in your database
- Node.js can collect form data
- Node.js can generate the dynamic page content
Installation
Download Node.js from its official website as per your operating system. Click on “Recommended for Most Users” to download the stable version. You can also install nvm for switching between multiple npm versions.
Npm includes a tool known as npx that runs the executable packages. Npx will help to create a react application in the directory that we run the command. We can create a new project by running the npx command which will also install the required dependencies. In simple words, npx is a package runner tool that’s included in npm.
To check the version of the node run the command
Node –v
To check the version of npm run the command
Npm –v
Demo
As we have now Node installed in our system, let’s try to create a simple demo using Node.js
Let’s start by creating a Hello world program with Node.js as our basic example.
Create a file named “HelloWorld.js”. Open the file and paste the code given below.
Example
const http = require('http');
const hostname = '127.0.0.1';
const port = 80;
const server = http.createServer(function(req, res) {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World!\n');
});
server.listen(port, hostname, function() {
console.log('Server running at http://' + hostname + ':' + port + '/');
});
The function has two parameters req and res which are the request from and response to the server, respectively.
Save the file at any desired location in your system.
The server listens to localhost on port 80 and prints "Server running at http://127.0.0.1:80/" in the command prompt.
Basically, the code written above indicates that if anyone tries to access our computer on port 80 as mentioned in the code then it will return “Hello World”.
To run our Node files, we first need to initiate it. To initiate it we need to open the command prompt (cmd) and navigate to the directory where our file is located.
Command to initiate,
path > node HelloWorld.js
As shown, the console shows a message that the server is running with the hostname and the port printed.
Now, start your internet browser, and type in the address: http://localhost:80 or http://127.0.0.1:80