-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
30 lines (25 loc) · 838 Bytes
/
app.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
const http = require('http');
const url = require('url');
const fs = require('fs');
const finalhandler = require('finalhandler');
const serveStatic = require('serve-static');
const checkStatus = require('./checkStatus.js');
const pageTemplate = fs.readFileSync(
'./client/index.html',
{ encoding: 'utf8' },
);
const serve = serveStatic('./client');
const server = http.createServer((req, res) => {
const parsedUrl = url.parse(req.url, true);
if (parsedUrl.pathname === '/') {
checkStatus().then((washing) => {
const bodyClass = washing ? 'active' : 'inactive';
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(pageTemplate.replace('{{ bodyClass }}', bodyClass));
});
} else {
const done = finalhandler(req, res);
serve(req, res, done);
}
});
server.listen(process.env.PORT);