Getting started with NodeJs

Purti Aggarwal
Aeologic
Published in
5 min readJun 20, 2020

NodeJs came about when the developer took javascript something that at that point we could only run in the browser and they allowed it to run as a standalone process on your machine. So before NodeJs javascript just was used as a more general-purpose programming language. With the help of the NodeJs now Javascript could build the web-server and connect the application to the database.

In this blog, we will cover the following areas of NodeJS:

  1. Introduction of NodeJS.
  2. Installation of NodeJS and NPM.
  3. Installation of VS Code.
  4. Basic project setup with express.
  5. Hello World” with NodeJs

What is NodeJs?

NodeJs is a free open source server that allows us to run Javascript on the server. With the help of NodeJs, we can create, open, read, write, delete, and close files on the server. We can generate dynamic websites and add delete our data from the database.

For developing a NodeJs application we require the following tools.

  1. NodeJs
  2. Node Package Manager (NPM)
  3. IDE (Integrated Development Environment) or TextEditor

The Modules in NodeJs have simple or complex functionalities all are organized in single or multiple Js files which can be reused in the NodeJs application project.

Each module in NodeJs has its different context, so it cannot interfere with other modules or pollute the global scope.

NodeJs Module Types:

NodeJs includes three types of modules:

1. Core Modules:

The Core Modules are those modules that coded into the binary distribution and loaded whenever the NodeJs process starts. However, we need to import the module to use the module in your application.

2. Local Modules:

The Local Modules are those modules that we create locally for the application project. In other words, we can say the different functions that we created for the different functions of the application project.

3. Third-Party Modules:

The Third-Party Modules are those modules that can be downloaded through the npm. These modules are created by someone else but we can use it in our project.

Let’s start building the project.

Step-1: Download the required tools

First, we download the NodeJs from the given link https://nodejs.org/en/

For making sure that NodeJs downloaded correctly, for that open terminal or CMD and run the node -v command as below.

$ node -v
v13.12.0

It will show the version of the NodeJs installed. If you are not getting this it means you didn’t download the tool correctly.

NPM is included in NodeJs installation, so there is no need to install it separately.

Second, we need to download the Texteditor tool where we write our code.

We use the Visual Studio code editor. So download the editor from the given link https://code.visualstudio.com/download

Step-2: Creating the directory

First, we have to make the base folder in which we have all the project related directories. An example we made the application folder.

Second, now we make the src(source) folder. This folder contains the server-side specific files. Inside the src folder create a new file index.js that will be the entry page for the application.

File Structure

Step-3: Installing the express module

To use NodeJs NPM modules, you first to install the module in the terminal using below command

$ npm i express

Here we install the Express Module.

Step-4: Import the Module

Now we have to load the module in our project (index.js file).

const express = require('express');const app = express();

In the above example, require() function returns object because the express module returns its functionality as an object.

Step-5: Listen to the Port

Now we have to bind and listen for connections on the specified host and port.

First, we have to declare the port inside the index.js file.

...
const port = process.env.PORT || 3000

In the above, we set the environment variable PORT or the port will be 3000.

Second, we have to listen to the port that should be declared at the end of the page with the callback the function to console the statement.

...
app.listen(port, () => {
console.log('Server is up to ' + port)
}

Step-6: Route the request

Now we send the request to the server

We are using the app object with the callback function which is called whenever the server receives the request

...
app.get('/', (req, res) => {
req.send('Hello World');
}
...

Step-7: Final Step

Run the command in the terminal.

$ node index.js
The result after the server is up

You will see the application is served on localhost on port 3000 and it is returning “Hello World”.

Summary

In this blog, we learned about NodeJs and it’s the express framework. First, we have set up the file structure then we see the module installation and how to route the request to the server. NodeJs has owns a huge world. We can do many things with NodeJs like make the Authenticate the website. The dataBase can be linked to the backend where we can save the data of the registered users and many other things. Hopefully, this article helps you with getting started with the NodeJs.

For more learning, you should go through the link https://nodejs.org/docs/latest-v13.x/api/synopsis.html

Feel free to connect with us read more articles from Aeologic.com.

Aeologic Technologies is a dynamic, solution, and value driven technology company. We position ourselves as a new generation cutting edge technology company. We work creatively to enable businesses with leading technologies and solutions. You can connect with us on Facebook, Twitter, and LinkedIn for any queries.

--

--