-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
72 lines (63 loc) · 2.56 KB
/
server.js
File metadata and controls
72 lines (63 loc) · 2.56 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const express = require('express');
const app = express();
const path = require('path');
const { spawn } = require('child_process');
app.use(express.static('public'));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
const PORT = process.env.PORT || 3000;
// Listen on all interfaces by default. When printing the URL, prefer
// 'localhost' if the server is bound to 0.0.0.0 or :: so the link is
// directly clickable from the machine running the server.
const HOST = process.env.HOST || '0.0.0.0';
const server = app.listen(PORT, HOST, () => {
const addr = server.address();
const address = addr && addr.address ? addr.address : HOST;
const port = addr && addr.port ? addr.port : PORT;
const displayHost = (address === '::' || address === '0.0.0.0') ? 'localhost' : address;
console.log(`Server running on port ${port}`);
console.log(`Open: http://${displayHost}:${port}/`);
// Start Tailwind watcher in development unless explicitly disabled
let tailwindProc = null;
function startTailwindWatcher() {
const isProd = process.env.NODE_ENV === 'production';
const disabled = process.env.START_TAILWIND === 'false';
if (isProd || disabled) {
console.log('Tailwind watcher: not started (production or disabled)');
return;
}
try {
const args = [
'tailwindcss',
'-i',
'./public/css/tailwind-input.css',
'-o',
'./public/css/tailwind.css',
'--watch',
];
console.log('Starting Tailwind CSS watcher via npx:', ['npx'].concat(args).join(' '));
tailwindProc = spawn('npx', args, { stdio: 'inherit' });
tailwindProc.on('error', (err) => {
console.error('Tailwind watcher error:', err && err.message ? err.message : err);
});
tailwindProc.on('exit', (code, signal) => {
console.log(`Tailwind watcher exited${signal ? ' with signal ' + signal : ' with code ' + code}`);
tailwindProc = null;
});
} catch (e) {
console.error('Failed to spawn Tailwind watcher:', e && e.message ? e.message : e);
}
}
function stopTailwindWatcher() {
if (tailwindProc && !tailwindProc.killed) {
console.log('Stopping Tailwind watcher...');
try { tailwindProc.kill('SIGTERM'); } catch (err) { /* ignore */ }
}
}
// ensure we clean up the watcher on process termination
process.on('exit', stopTailwindWatcher);
process.on('SIGINT', () => { stopTailwindWatcher(); process.exit(0); });
process.on('SIGTERM', () => { stopTailwindWatcher(); process.exit(0); });
startTailwindWatcher();
});