6. Tools
1. nodemon
Tool
- It watches your server-side files (like
app.js
) for changes. - It automatically restarts the Express server when changes are detected.
- This saves you from stopping and restarting the server manually.
β What nodemon
doesnβt do:
- It does not auto-refresh your browser.
- Itβs not like frontend dev tools (e.g., Vite, Webpack Dev Server) which support hot reload or live reload in the browser.
π§ͺ Example Workflow
- You edit
app.js
or your routes nodemon
restarts the server automatically- β Your new code is now running
- π But you still need to refresh your browser to see the updated result
2. Auto Browser Refresh
You can set up automatic browser refresh alongside nodemon
using a tool like livereload
or browser-sync
with Express.
2.1 π Option 1
Use livereload
+ connect-livereload
(simple setup)
π¦ Step 1: Install dependencies
npm install --save-dev livereload connect-livereload
π Step 2: Setup LiveReload in app.js
const express = require('express');const app = express();const path = require('path');
// Livereloadconst livereload = require('livereload');const connectLivereload = require('connect-livereload');
// 1. Create livereload serverconst liveReloadServer = livereload.createServer();liveReloadServer.watch(path.join(__dirname, 'public'));
// 2. Add connect-livereload middlewareapp.use(connectLivereload());
// 3. Trigger refresh on changeliveReloadServer.server.once('connection', () => { setTimeout(() => { liveReloadServer.refresh('/'); }, 100);});
// Serve static filesapp.use(express.static('public'));
app.get('/', (req, res) => { res.sendFile(path.join(__dirname, 'public/index.html'));});
app.listen(3000, () => { console.log('Server running at http://localhost:3000');});
π Step 3: Make sure your HTML includes this line:
In public/index.html
, add just before </body>
:
<script src="http://localhost:35729/livereload.js"></script>
β Result:
- Any time you change a file in the
public/
directory (e.g. HTML/CSS/JS), the browser auto-refreshes. nodemon
still handles backend restarts.
You can still run your app with:
npx nodemon app.js
It restarts the server when Express code changes, and livereload
refreshes the browser for public file changes.
2.2 π§ͺ Option 2: browser-sync
β What youβll get:
- Browser auto-refresh on file changes (HTML/CSS/JS/backend)
- Multi-device testing (synchronize clicks, scrolls, inputs)
- Runs alongside
nodemon
π§© 1. Install Required Packages
npm install browser-sync --save-devnpm install nodemon --save-dev
π Example Project Structure
project/βββ public/β βββ index.htmlβββ app.jsβββ bs-config.jsβββ package.json
π 2. Basic Express Server (app.js
)
const express = require('express');const app = express();const path = require('path');
app.use(express.static('public'));
app.get('/', (req, res) => { res.sendFile(path.join(__dirname, 'public/index.html'));});
app.listen(3001, () => { console.log('Express server on http://localhost:3001');});
βοΈ 3. Create bs-config.js
(BrowserSync Config)
module.exports = { proxy: "http://localhost:3001", // Your Express app port: 3000, // BrowserSync will serve on this files: ["public/**/*.*"], // Watch these files for reload open: true, // Auto-open browser reloadDelay: 500 // Give time for nodemon to restart};
π 4. Update package.json
Scripts
"scripts": { "dev": "nodemon app.js", "sync": "browser-sync start --config bs-config.js", "start": "concurrently \"npm:dev\" \"npm:sync\""}
Install concurrently
if you donβt have it:
npm install concurrently --save-dev
π 5. Start the App
npm start
- Express runs on port
3001
- BrowserSync serves on port
3000
- Changes to
public/
files auto-refresh the browser - Backend restarts with
nodemon