-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
34 lines (29 loc) · 1.02 KB
/
Copy pathserver.js
File metadata and controls
34 lines (29 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const path = require('path');
const express = require('express');
const http = require('http');
const WebSocket = require('ws');
const chokidar = require('chokidar');
const app = express();
const publicDir = path.join(__dirname, '.');
app.use(express.static(publicDir));
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
wss.on('connection', (ws) => {
console.log('Live-reload client connected');
});
// Watch project root for changes (ignore node_modules and .git) and notify clients to reload
const watcher = chokidar.watch(publicDir, {
ignored: /(^|[\/\\])(node_modules|\.git)/,
ignoreInitial: true,
});
watcher.on('all', (event, filePath) => {
console.log('File change detected:', event, filePath);
const msg = JSON.stringify({ type: 'reload' });
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) client.send(msg);
});
});
const port = process.env.PORT || 3000;
server.listen(port, () => {
console.log(`Serving ${publicDir} at http://localhost:${port}`);
});