How to add auto-reload to your Node JS app?

I am a developer from Delhi, India
Search for a command to run...

I am a developer from Delhi, India
No comments yet. Be the first to comment.
DISCLAIMER: This might not be the most well-written article since I haven't written in a while and I just wanted to get back to writing with a short article about my experience of the day, so sorry about that I previously used this repository for my...

The release of NextJS 13 brought about a plethora of new and impressive features, with one standout being the updated data fetching and management process. The fetch API replaced the more complicated functions, including getServerSideProps, getStatic...

Looking for a safe and reliable way to authenticate users? Consider implementing magic links. They offer a secure alternative to traditional passwords and can help mitigate the risk of password leaks and forgotten passwords. Magic Link authentication...

Neovim is a powerful text editor that can be customized with plugins to enhance its functionality. In this article, we will explore the top 10 essential Neovim plugins. These plugins can help improve your Neovim experience by adding features such as ...

A type-safe and zero-runtime version of Tailwind CSS

This is one of the first things I searched for, when I started learning Node JS with Express, since it was becoming too hard of a job to keep stopping and re-running the server, again and again. This is why, I use an npm package called nodemon
I hope you have NodeJS installed in your machine.
mkdir nodemon_tutorial && cd nodemon_tutorial
npm init -y
This would create a package.json
- Now, let's install express to start a server
npm install express
Now, let's create a file named index.js
touch index.js
Open the folder in your favourite Code Editor. I use VS Code, so I will run:
code .
Now inside index.js add the following
const express = require('express');
const app = express();
const port = process.env.PORT || 3000
app.get('/', (req, res) => {
res.send("Hello World!");
})
app.listen(port, () => {
console.log(`App is running at port: ${port}`);
})
PORT. If it does not find any, by default, it assigns it to 3000.get request to /, then he will get Hello World as responsenode index.js
This will give the output
App is running at port: 3000
localhost:3000/"Hello World!" to "Hello, this is my first nodemon app!"npm install nodemon --save-dev
We are adding --save-dev because we want this only in development and not while publishing it.
Now, go to package.json file and edit the following line:
- "test": "echo \"Error: no test specified\" && exit 1"
+ "start":"nodemon index.js"
Now, terminate the server which was running and run:
npm start
Now, go to http://localhost:3000
index.js and after you save it, the server should auto-reload. Now go to the browser and refresh again. You should see the new response