Setup Express in Node.js

Node.js is an open source server environment that allows us to use JavaScript. Express is a minimal and flexible Node.js web application framework to provide server side functionality for web and mobile applications. Let’s start by creating a folder where you want your express server to be running.

Execute the following command in your command prompt.

mkdir express-project

Then you need to initialize the package.json file in that folder to manage the dependencies. To do that you need to run the command below. The command will ask certain questions to help create the package.json file.

npm init

If you want to skip answering the questions and create the default package.json file, then execute the command: npm init -y

The package.json file will look something like this:

  "name": "express-project",
  "version": "1.0.0",
  "description": "Setup the express server",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  "author": "Anushree Dave",
  "license": "ISC"

Let’s install express in our project and save it in our package.json file. This helps us share our project with other developers.

npm install express --save

The command above will create node_modules folder in your project directory. Let’s create server.js file in your project directory. This file will be the entry point for our project. We need to setup our entry point file, where you can write the lines of code below:

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

const server_port = 3000;

app.use('/', (req, res, next) => {
    res.send("welcome to express project");

//Display it when the server has started
app.listen(server_port, () =>
  console.log(`The server is running on http://localhost:${server_port}`)

We can start the server by executing this command

node server.js

You can visit http://localhost:3000 and will see the response as “welcome to express project”

I hope this post was helpful to you. If you like my post, follow me @twitter/andramazo.

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top