How to setup Node-Express server with nodemon(Auto refresh when you make any change)

Today I am going to show you how you can setup your node-express server with nodemon for auto refreshing when you make any change in your file.

 

First thing you need to create a folder named what are you like In my case I am going to put my folder name is backend.

 

Make sure you have installed node in your machine.

 

Now inside this folder open terminal and execute some command.

npm init -y

This command will create a JSON file with some information.This command basically initialize your node application.

 

We are ready to install nodemon and express js

npm i express nodemon

Installation is done...!

 

Create a file called server.js or you can use any name inside your root folder(in my case this is backend,if you don't create inside root folder, then this setup will not work).Open and setup like bellow.

import express from 'express';
const app = express();

app.get('/',(req,res)=>{

res.send("Hello World");

})

app.listen(5000, () => {
  console.log('Server is runing');
});

 

Now, just you need to do some configuration in your packege.json file which is created by executing our first command.

 

Let's open your package.json file.And setup like bellow.Only you need to change  "scripts"  to "start": "nodemon server.js"

{
  "name": "backend",
  "type": "module",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "nodemon server.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1",
    "nodemon": "^2.0.13"
  }
}

 

Done...

 

Let's run our node application

npm start

 

Open your browser and go to 'http://localhost:5000' you should see Hello World in your browser.If you make any change in server.js file and save immediately you can see this change in browser.

 

About Author

Photo

Hi, I am Mahbub Hasan
Software Engineer

I am very much interested to share my programming
and development knowledge with the people.I will try
to update my blog everyday with new technology inshallah.

Thank You

For Visiting My Blog.

Contact Me