-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
26 lines (21 loc) · 712 Bytes
/
index.js
File metadata and controls
26 lines (21 loc) · 712 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
const jsonServer = require('json-server');
const server = jsonServer.create();
const middlewares = jsonServer.defaults({static: 'public'});
const fs = require('fs');
const csv = require('csv-parser');
server.use(middlewares);
server.get('/stocks/:sym', (req, res) => {
const filename = `data/${req.params.sym.toUpperCase()}.csv`;
const results = [];
// adapted from example in: https://www.npmjs.com/package/csv-parser
fs.createReadStream(filename)
.on('error', () => res.status(404).end())
.pipe(csv())
.on('data', data => results.push(data))
.on('end', () => {
res.json(results);
})
});
server.listen(3000, () => {
console.log('Running at: http://localhost:3000');
});