- read

How to Spin Up a local server using ExpressJS under 5 mins

Jay Panchal 2

It is comparetively easy to spin up a server using ExpressJS than trying to use plain NodeJS

To create a server using Express, lets first get the package from npm.

Use the below command to get the express module from npm

npm install express

After successfull installation of the module, make a new app.js file.

Creating Server

Inside app.js file .We can create the server by just invoking express() as shown below

const express = require(‘express’);

const app = express();

another common convention developers use is as follows: (Note that they both have the same functionality)

const app = require(‘express’)();

Listening to Port:

Now lets add a port that our server will be listening to:

app.listen(5000, ()=>{console.log(‘Server is listening on port 5000’)});

here the callback function is optional. Our app is now listening to port 5000.

Our server is ready, but you’ll get an error when trying to access on


because we haven’t set any response for that get request yet.

Adding Response:

Now lets add a response for the request to check whether this blog is legit or not 😋

We have a get method on our server that takes 2 parameters,

1st is path and 2nd is a callback function with request, response parmas

app.get(‘/’, (req, res)=> {res.send(‘Hello World, The Blog is legit’)});

refresh the browser http://localhost:5000/ and check if you can see the text now

Whallaaaa you now have a working server for yourself.


Adding a Response for 404:

To add a response for any other request or URL that a user may try to browse that is not there, instead of http://localhost:5000/

It is very simple using ExpressJS

app.all(“*”, (req, res) => {res.status(404).send(“404 Page Not Found”);});

Here we are explicitly adding 404 status code, express can take care of it automatically. But i am showing that we can do that also

Here * indicates all other requests that user may browse other than the one that we have responded.

So now if you browse to any other link it will show 404 Page Not Found

try http://localhost:5000/asasas

Thanks for the Read 😄