-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
49 lines (42 loc) · 1.2 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
import express from 'express';
import bodyParser from 'body-parser'
import chokidar from 'chokidar'
import fs from 'fs'
var sleep = require('sleep');
let app = express();
app.disable('x-powered-by');
let rootDir = __dirname + '/..';
app.use(bodyParser.json())
app.use(express.static(rootDir + '/public'))
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.get('/', (req, res) => {
res.render('index', { title: 'ejs' });
});
var mostRecentFile = ''
var data = []//JSON.parse(fs.readFileSync(mostRecentFile, 'utf8'))
chokidar.watch('./', {encoding: 'buffer'}).on('all', (event, path) => {
if (event === 'add' && path.indexOf('.hlt') > 0) {
if (mostRecentFile != path) {
setTimeout(function() {
mostRecentFile = path
while (true) {
try {
data = JSON.parse(fs.readFileSync(path, 'utf8'));
return
} catch (e) {
sleep.usleep(250)
}
}
}, 500)
}
}
});
app.get('/latest', (req, res) => {
res.json({ name: mostRecentFile, data: req.query['f'] == mostRecentFile ? null : data })
});
(function() {
console.log('starting app');
return app.listen(8888);
})();
export default app;