-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
28 lines (24 loc) · 936 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
const http = require('http');
const Stock = require('./api/stock');
const Log = require('./utils/log');
const log = Log();
http.createServer((req, res) => {
if (/\/api\/stock\/[\w]+$/.test(req.url)) {
const ticker = req.url.match(/[\w?]+$/);
const stock = Stock({ name: ticker });
log.record(req.url).recordDate();
stock.getInfo().then((stockInfo) => {
log.recordStatus(stockInfo.statusCode).end();
res.writeHead(stockInfo.statusCode, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(stockInfo));
}).catch((err) => {
log.recordStatus(err.statusCode).end();
res.writeHead(err.statusCode, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(err));
});
} else {
log.record(req.url).recordDate().recordStatus(400).end();
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('Bad Request');
}
}).listen(3001, 'localhost');