-
Notifications
You must be signed in to change notification settings - Fork 195
/
main.js
58 lines (47 loc) · 1.27 KB
/
main.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
const http = require('http');
const path = require('path');
const express = require('express');
const socketio = require('socket.io');
const parseArgs = require('minimist');
const fs = require('fs');
const app = express();
const server = http.createServer(app);
const io = socketio(server);
const args = parseArgs(process.argv.slice(2));
const { name = 'default', port = '8080'} = args;
app.use(express.static(path.join(__dirname, 'public')));
app.get('/api/test', (req, res) => {
res.json({
headers: req.headers,
address: req.connection.remoteAddress
});
});
app.get('/api/name', (req, res) => {
res.json({ name });
});
app.get('/api/info', (req, res) => {
fs.readFile(`${__dirname}/version.txt`, 'utf8', (err, version) => {
res.json({
version: version || 0,
dirname: __dirname,
cwd: process.cwd()
});
});
});
io.on('connection', (sock) => {
console.log('Client connected');
sock.on('heartbeat', (payload) => {
payload.nodeName = name;
sock.emit('heartbeat', payload);
});
sock.on('disconnect', () => {
console.log('Socket Disconnected');
});
});
server.listen(+port, '0.0.0.0', (err) => {
if (err) {
console.log(err.stack);
return;
}
console.log(`Node [${name}] listens on http://127.0.0.1:${port}.`);
});