-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
40 lines (34 loc) · 943 Bytes
/
server.js
File metadata and controls
40 lines (34 loc) · 943 Bytes
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
const express = require('express');
const app = express();
const path = require('path');
const fs = require('fs');
const ndjson = require('ndjson');
const cors = require('cors');
app.use(cors())
app.use(express.static(path.join(__dirname, 'public')));
app.get('/data', (req, res) => {
let readStream = fs.createReadStream(__dirname + '/public/plot.ndjson').pipe(ndjson.parse());
const chunks = [];
readStream.on('data', (data) => {
chunks.push(JSON.stringify(data));
});
// readStream.on('end', () => {
// while (chunks.length) {
// res.write(chunks.shift() + '\n');
// }
// res.end();
// });
readStream.on('end', () => {
var id = setInterval(() => {
if (chunks.length) {
res.write(chunks.shift() + '\n');
} else {
clearInterval(id);
res.end();
}
}, 100);
});
});
app.listen(9000, () => {
console.log('Example app listening on port 9000!');
});