build express js server in nodejs
build express js server in nodejs

Express.js in JavaScript. The Express.js is the most popular web framework used for building web applications and APIs in Node.js Project. In this article we will learn how to create express server with nodejs. Before you start, make sure you have Node.js installed on your system. If not, you can download it from the official website:

1. Setup Node Project

Create a folder named express-server open the folder in your favorite code Editor (VSCode), then run below cmd to make it a Node Project.

#initialize a Node Project

npm init -y

This cmd will convert a normal folder into a node project by creating package.json file it holds your project meta-data.

2. Create a server file

In root directory of project, create a file by name index.js.

Now to make a Express Server application we need to download & install Node Express Module into the project.

Run below cmd to install ExpressJS

npm install express

now import it on top on index.js file to create Node Express Application

const express = require('express');

//create express app
const app = express();

3. Add Middleware

In our Node Project, Let’s use 2 middleware

  1. One is to parse incoming request with JSON payloads.
  2. Second is to parse incoming request with URL-encoder payloads
const express = require('express');
const app = express();

// parse incoming request to JSON payloads.

//parse incoming request with URL-encoding 

4. Create an API router

Now lets create a root API that will send JSON data response whenever there is a request to root api.


res.status(200).json({message:"API CALLED..!"})


5. Creating Express Server & Listen for server

Now let’s make a server to listen to a particular port may be let’s say post 3000 address i.e http://localhost:3000/

const port = 3000;

   console.log("Server listening to port 3000");

6. Start a server

To start Node Express Server, Open terminal/cmd prompt & run node index.js be being in root directory of project.

node index.js

7. Make a API request

Visit http://localhost:3000/ in your browser to see the API response.

Related Article

Books Directory Project wirh NodeJS – CRUD operation using Mongoose MongoDB

How does MERN Stack Work