Express JS 101: Spinning up a basic server

Written by Omar Mohammad|3 min read|

Node JS has become increasing popular over the last few years and a lot of people have turned to it as a platform of choice for backend development.

Node JS single threaded behavior makes it fast for basic Input-Output (while making the computation time a bit slower) but at the same time does so in a lightweight manner while also retaining a very extensible toolset.

In Node we use Node Package manager to add new libraries (that add new functionalities) to our Node project. One such library is called Express JS, which is nothing but a server side library that allows to quickly and easily set up Node servers without too much hassle.

Let us see how we can create a new project and express along with basic routing.


You must know basic terminologies associated with backend/server side programming.

You should have atleast Node JS 10.x.x LTS installed and the latest version of NPM.

Step 1: Project Setup

Create a new folder and inside it open terminal and type:

npm init --yes

This will initialize the basic Node/NPM settings for the project. You will see a package.json file that contains information about your project.

Now lets install express using NPM

npm install express

This command will install Express and you will see that the package.json will be updated and express will be listed under the dependency section.

Step 2: Hello world

Create a new index.js file in root and write the following code inside it.

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

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

app.listen(PORT, () => {
  console.log(`App listening on ${PORT}`);

The first line is how we get express functionality inside out file. Require serves the same purpose of an ES6 import.

Next in the second line we just give the imported express function a name called ‘app’. This is just so that it is easy to reference and use in the future.

Next we define the PORT. Now this is a bit tricky but what we’re essentially doing is that we’re telling node to use an environment variable if it is defined otherwise revert to the default port of 3000. Environment variables are an important consideration when deploying our express app.

Finally we write the app.listen and tell express to start the server. We pass the PORT variable and a callback as an argument, the callback has a console.log() that tells us the server has started.

Now if you go ahead and type in your terminal:

node index.js

Open http://localhost:3000 in your browser and check the console.

You should see in the console.

App listening on 3000

Great! You have now build your first express App!