-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
79 lines (56 loc) · 1.72 KB
/
Copy pathserver.js
File metadata and controls
79 lines (56 loc) · 1.72 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
73
74
75
76
77
78
79
const express = require('express')
const next = require('next')
const bonjour = require('bonjour')
const ObjectCreator = require('./object-creator.js');
const SerialHandler = require('./serial-handler.js');
const fs = require('fs');
require.extensions['.properties'] = function (module, filename) {
module.exports = fs.readFileSync(filename, 'utf8');
};
const constants = require('./components/constants.js');
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
var ttl_port;
const recentInformation = new ObjectCreator('database.db', constants);
const port = process.env.PORT || 5000;
const serialHandler = new SerialHandler(recentInformation);
serialHandler.initSerialPort();
app.prepare()
.then(() => {
const server = express()
const bj = bonjour()
server.get('/data/:page', (req, res) => {
const page = { title: req.params.page }
res.send({data: recentInformation.buildData(page.title)});
});
server.get('/plot/', (req, res) => {
var ids = []
ids = ids.concat(req.query.id);
res.send({data: recentInformation.buildPlotData(ids)})
});
// server.get('/constants/', (req, res) => res.send(constants));
server.get('*', (req, res) => {
return handle(req, res)
})
const port = 80;
// const service = bj.publish({name: 'NK319', port: port, type: 'http'});
server.listen(port, (err) => {
if (err) throw err
console.log('> Ready on http://localhost:3000')
})
})
.catch((ex) => {
console.error(ex.stack)
process.exit(1)
})
process.on('SIGTERM', () => {
server.close(() => {
console.log('Process terminated')
})
if (ttl_port) {
ttl_port.close(() => {
console.log('Porta fechada')
})
}
})