-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
79 lines (69 loc) · 1.89 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const http = require('http')
const pug = require('pug')
const express = require('express')
const path = require('path')
const bodyParser = require('body-parser')
exports.makeServer = ({ getWebData, reset, setPaused, port }) => {
const app = express()
const httpServer = http.createServer(app)
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'pug')
app.set('x-powered-by', false)
app.engine('pug', pug.renderFile)
app.use(express.static(path.join(__dirname, 'static')))
app.use(bodyParser.json())
app.get('/', function (req, res, next) {
res.render('index')
})
app.get('/data', (req, res, next) => {
// returns once reading done
getWebData().then((data) => {
res.setHeader('Content-Type', 'application/json')
res.send(JSON.stringify(data))
}, (error) => {
res.status(500).render('error', {
title: '500 Server Error - hottub.local',
message: error.message ?? error
})
})
})
app.post('/reset', (req, res, next) => {
reset().then(() => {
res.status(200).send('ok')
}, (error) => {
res.status(500).render('error', {
title: '500 Server Error - hottub.local',
message: error.message ?? error
})
})
})
app.post('/setPaused', (req, res, next) => {
setPaused(req.query.pause === 'true').then(() => {
res.status(200).send('ok')
}, (error) => {
res.status(500).render('error', {
title: '500 Server Error - hottub.local',
message: error.message ?? error
})
})
})
app.get('*', function (req, res) {
res.status(404).render('error', {
title: '404 Page Not Found - hottub.local',
message: '404 Not Found'
})
})
// error handling middleware
app.use(function (err, req, res, next) {
console.error(err.stack ?? err.message ?? err)
res.status(500).render('error', {
title: '500 Server Error - hottub.local',
message: err.message ?? err
})
})
return {
run: () => {
httpServer.listen(port)
}
}
}