6. Tools
1. nodemon Tool
Section titled β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:
Section titled ββ 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
Section titled βπ§ͺ Example Workflowβ- You edit
app.jsor your routes nodemonrestarts 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
Section titled β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
Section titled β2.1 π Option 1βUse livereload + connect-livereload (simple setup)
π¦ Step 1: Install dependencies
Section titled βπ¦ Step 1: Install dependenciesβnpm install --save-dev livereload connect-livereloadπ Step 2: Setup LiveReload in app.js
Section titled βπ 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:
Section titled βπ 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:
Section titled ββ Result:β- Any time you change a file in the
public/directory (e.g. HTML/CSS/JS), the browser auto-refreshes. nodemonstill handles backend restarts.
You can still run your app with:
npx nodemon app.jsIt restarts the server when Express code changes, and livereload refreshes the browser for public file changes.
2.2 π§ͺ Option 2: browser-sync
Section titled β2.2 π§ͺ Option 2: browser-syncββ What youβll get:
Section titled ββ 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
Section titled βπ§© 1. Install Required Packagesβnpm install browser-sync --save-devnpm install nodemon --save-devπ Example Project Structure
Section titled βπ Example Project Structureβproject/βββ public/β βββ index.htmlβββ app.jsβββ bs-config.jsβββ package.jsonπ 2. Basic Express Server (app.js)
Section titled βπ 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)
Section titled ββοΈ 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
Section titled βπ 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
Section titled βπ 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