Short answer
You can’t “force” a tab in Chrome/Edge/Firefox to reload from Node’s http module once the response has finished—HTTP is request/response and then the connection is done.
What you can do is keep a second, long-lived connection (WebSocket or SSE) open and tell the page, “Hey, reload yourself,” or just use an off-the-shelf tool that already does exactly that.
Below are three levels of “simplicity”:
────────────────────────────────────────
- Easiest: use a ready-made CLI (zero code)
────────────────────────────────────────
Install live-server globally (or locally via npx):
npm i -g live-server # global
# or
npx live-server # without global install
Then just run
live-server .
• Watches the current folder for file changes
• Injects a tiny script into every HTML file it serves
• When something changes, that script receives a WebSocket ping and does location.reload() for you.
BrowserSync, vite, parcel, etc. do the same, but live-server is the tiniest.
────────────────────────────────────────
2) Still tiny: roll your own in ±25 lines
────────────────────────────────────────
If you’d rather keep your own Node http server and add the reload yourself:
server.js
const http = require('http');
const fs = require('fs');
const WebSocket = require('ws');
const server = http.createServer((req, res) => {
// serve a tiny reload helper
if (req.url === '/reload.js') {
res.writeHead(200, {'Content-Type':'application/javascript'});
return res.end(`
const ws = new WebSocket('ws://' + location.host);
ws.onmessage = () => location.reload();
`);
}
// any static file you want (super quick & dirty)
const file = req.url === '/' ? '/index.html' : req.url;
fs.createReadStream(__dirname + file).pipe(res);
});
const wss = new WebSocket.Server({ server });
// watch your project; on change tell all connected clients
fs.watch(__dirname, { recursive: true }, () => {
wss.clients.forEach(c => c.readyState === 1 && c.send('reload'));
});
server.listen(3000, () => console.log('http://localhost:3000'));
index.html (or whatever you serve):
<!doctype html>
<html>
<head><meta charset="utf-8"><title>My page</title></head>
<body>
<h1>Hello!</h1>
<script src="/reload.js"></script>
</body>
</html>
Whenever any file inside the folder changes, fs.watch fires → server pushes “reload” via WS → browser calls location.reload().
No build tools, no npm scripts—just Node + ws.
────────────────────────────────────────
3) Crude but zero dependencies: timed refresh
────────────────────────────────────────
If you don’t actually need “change-only” reloads, you can stick a meta refresh or a JS timeout in your HTML:
<meta http-equiv="refresh" content="5"> <!-- reload every 5 s -->
or
<script>setTimeout(()=>location.reload(),5000);</script>
That hits the server again every N seconds. Simple, but wasteful and not “server-driven.”
────────────────────────────────────────
Take-aways
• Plain HTTP can’t poke the browser after the response is sent.
• Use WebSockets, SSE, or an existing live-reload tool.
• The one-liner solution is literally npx live-server.
Pick the level of “simple” that matches your patience 🙂. Happy coding!