-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.js
40 lines (34 loc) · 966 Bytes
/
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
const express = require('express');
const fs = require('fs');
const app = express();
const port = process.env.PORT || 3001;
const { Transform } = require('stream');
app.get('/api', (req, res) => {
const stream = fs.createReadStream('./constants/orders.txt');
const splitLines = new Transform({
readableObjectMode: true,
transform(chunk, encoding, cb) {
this.push(chunk.toString().trim().split('\n'));
cb();
}
});
const splitOrder = new Transform({
readableObjectMode: true,
writableObjectMode: true,
transform(orders, encoding, cb) {
let parsed = orders.map((order) => {
let t = order.split('\t');
t[1] = parseInt(t[1]);
t[2] = parseInt(t[2]);
return t;
});
this.push(JSON.stringify(parsed) + '\n');
cb();
}
});
stream
.pipe(splitLines)
.pipe(splitOrder)
.pipe(res);
});
app.listen(port, () => console.log(`Listening on port ${port}...`));