-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
63 lines (54 loc) · 1.73 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import http from 'http';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { spawn } from 'child_process';
import chalk from 'chalk';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
let testProcess;
const PORT = process.env.PORT || 3980;
const startServer = () => {
const server = http.createServer((req, res) => {
if (req.url === '/') {
fs.readFile(path.join(__dirname, 'index.html'), (err, content) => {
if (err) {
console.error('Error reading index.html', err);
res.writeHead(500);
res.end('Error loading the page');
} else {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(content);
}
});
} else {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('404 Not Found');
}
});
server.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
testProcess = spawn('node', [path.join(__dirname, './index.js')], {
stdio: 'inherit'
});
testProcess.on('close', (code) => {
console.log(`Bot Server process exited with code ${code}`);
});
setInterval(() => {
console.log('Restarting server and bot process...');
testProcess.kill('SIGINT');
server.close(() => {
server.listen(PORT, () => {
console.log(`Server restarted on port ${PORT}`);
testProcess = spawn('node', [path.join(__dirname, './index.js')], {
stdio: 'inherit'
});
testProcess.on('close', (code) => {
console.log(`Bot Server process exited with code ${code}`);
});
});
});
}, 1800000);
});
};
startServer();