forked from vikingeducation/project_fideligard_spa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
52 lines (43 loc) · 1.34 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
50
51
52
// Require es6-promise polyfill and isomorphic-fetch
require("isomorphic-fetch");
// Express
const express = require("express");
const app = express();
// Set development port to 3001
app.set("port", process.env.PORT || 3001);
// When in production, only serve static assets
// from the client/build folder
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
}
// Hydration Endpoint
app.get("/api/stocks", (req, res, next) => {
try {
res.json(require("./stockData.json"));
} catch (error) {
next(error);
}
});
// Fetch Endpoint
const { fetchParsedRecords } = require("./quandl");
app.get("/api/stocks/fetch", async (req, res, next) => {
try {
let { start, end, columns, tickers } = req.query;
columns = columns ? columns.split(",") : columns;
tickers = isNaN(tickers) && tickers ? tickers.split(",") : tickers;
[start, end] = [+start, +end];
res.json(await fetchParsedRecords({ start, end, columns, tickers }));
} catch (error) {
console.log("catching");
next(error);
}
});
// Handle errors
app.use((err, req, res, next) => {
console.error("Error: ", err.stack);
res.status(err.response ? err.response.status : 500);
res.json({ error: err.message });
});
app.listen(app.get("port"), () => {
console.log(`Find the server at: http://localhost:${app.get("port")}/`);
});