-
Notifications
You must be signed in to change notification settings - Fork 25
/
server.js
48 lines (40 loc) · 1.18 KB
/
server.js
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
const express = require('express');
const path = require('path');
const chokidar = require('chokidar');
const { exec } = require('child_process');
const app = express();
const PORT = 3000;
// Serve static files from the 'output' directory
app.use(express.static(path.join(__dirname, 'output')));
// Function to regenerate HTML
const regenerateHtml = () => {
exec('node top-artists.js', (error, stdout, stderr) => {
if (error) {
console.error(`Error: ${stderr}`);
return;
}
console.log(stdout);
});
exec('node top-songs.js', (error, stdout, stderr) => {
if (error) {
console.error(`Error: ${stderr}`);
return;
}
console.log(stdout);
});
};
// Initial HTML generation
regenerateHtml();
// Watch for changes in 'top-artists.njk'
const watcher = chokidar.watch(
path.join(__dirname, 'views', 'top-artists.njk'),
path.join(__dirname, 'views', 'top-songs.njk')
);
watcher.on('change', () => {
console.log('Detected change in files. Regenerating HTML...');
console.log('Detected change in top-songs.njk. Regenerating HTML...');
regenerateHtml();
});
app.listen(PORT, () => {
console.log(`Server is running at http://localhost:${PORT}`);
});