Getting Started with NodeJS and Express

Image for post
Image for post

Before starting with the basic setup for NodeJS, first we need to know what is it and why we are using it.

NodeJS is a javascript runtime environment.

As we know, javascript needs a browser to run. But with NodeJS, we can use it as a scripting language which can be run in a server. This makes it easy for someone with knowledge of javascript, especially frontend developers, to try their hands on backend development like making API’s which will enable them to become a full stack developer in a short amount of time.

NodeJS installation :

Ubuntu :

Windows :

Mac :

Go to and click on macOS Installer. A package will get downloaded. Install it.

To verify if node is installed in you mac, open terminal and type ‘node -v’.

Also, we will be using npm as our package manager. You can use anything else as well with which you are comfortable.

Once you have successfully installed NodeJS in your system, we will proceed to the next step of creating a simple NodeJS project using Express.

Express is a NodeJS framework. That’s all we need to know for getting started.

Build your first NodeJS project using express :

Go to your desired folder and enter :

npm init

Next it will ask you to enter few details about your project like the package name, version, description, etc. It will also ask for an entry point i.e. from which file your application will start. Conventionally, it’s named index.js but we can use any name. This will be your entry point.

Image for post
Image for post

Now, once we have created package.json file, we are going to install the necessary packages to help us create our server. These packages, also called dependencies, after installation are auto added to the package.json file. Also, we can manually write the package name inside the package.json file and then run npm install to install all the packages at once.

For an editor, we are going to use VS Code, but we can use any available editors for this.

We are going to open our project directory in editor and open package.json file.

At first, the file will look something like this :

Image for post
Image for post

Now we will add dependencies to our project.

npm install express

This will install express in our project.

Now our package.json file will look something like this :

Image for post
Image for post

The express version is the latest version used here. Also the ^ before the version number specifies auto update whenever we install all the packages.

Once we have installed express, the node_modules folder will be created which contains all the necessary files required for a particular package

Now we will create our index.js file.

Image for post
Image for post

Lets go step by step about the code.

First we need to require our express module. For that we used

const express = require(‘express’);

Then we call the express function and assign it to a constant ‘app’;

By default, the port is 3000. So even if we don’t assign it, it will take 3000 as port. If we want to run it in a different port, just assign port constant to a different port.

We will be setting up our route via which we will access the server side from client side.

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

This means, we try to open http://locahost:3000/ in a browser, this part of the code will be executed.

This is an ES6 syntax that we are following here.

The above means, if we try to open http://locahost:3000/ in a browser, this part of the code will be executed as we have specified that if the server gets a path of ‘/’ then do the following.

Any request to the server can be accessed from the parameter ‘req’.

And to send a response to the client use :

res.send(‘Hello world’);

Now, to make the server listen on the desired port ,

app.listen(port, () => {
console.log(`Example app listening on port ${port}!`);

This part of code starts the express server

To run the code, go to terminal and go to the project directory and type :

node index.js

You can see the Example app listening on port 3000! message in console.
This means you have successfully started your server.


You can add a start script in the package.json file

Image for post
Image for post

“start” : “node index.js”

Go to terminal and type :

npm start

Now, go to browser and enter http://localhost:3000/ in url

Image for post
Image for post

You can see the message Hello World! as sent from your server.

Hope this tutorial helps you to get started with NodeJS using express

Full stack developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store