-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
81 lines (72 loc) · 2.71 KB
/
index.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const fetch = require("node-fetch");
const cors = require("cors");
app.use(cors());
app.options('*', cors());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
require("dotenv").config();
const timePeriod = require("./constants");
app.post("/stock", cors(), async (req, res) => {
const body = JSON.parse(JSON.stringify(req.body));
const { ticker, type } = body;
console.log("stocks-api.js 14 | body", body.ticker);
const request = await fetch(
`https://www.alphavantage.co/query?function=${timePeriod(
type
)}&symbol=${ticker}&apikey=${process.env.ALPHA_VANTAGE_API_KEY}`
);
const data = await request.json();
res.json({ data: data });
});
app.post("/stocks", async (req, res) => { //less than 5 stocks per minute
const body = JSON.parse(JSON.stringify(req.body));
const { tickers, type } = body;
console.log("stocks-api.js 14 | body", body.tickers);
let stocks = await tickers.map(async ticker => {
const request = await fetch(
`https://www.alphavantage.co/query?function=${timePeriod(type)}&symbol=${ticker}&apikey=${process.env.ALPHA_VANTAGE_API_KEY}`
);
const data = await request.json();
return data;
});
Promise.all(stocks)
.then(values => {
console.log("stocks-api.js 40 | values", values);
if (values[0].Note) {
console.log("stocks-api.js 48 | error", values[0].Note);
res.json({ error: values[0].Note });
} else {
res.json({ data: values, status: "done" });
}
})
.catch(error => {
console.log("stocks-api.js 47 | error", error);
res.json({ error: error });
});
});
app.post("/stocks-unlimited", async (req, res) => {//unlimited stocks in 12 seconds X number of tickers (i.e 10 tickers = 120 seconds to get data.)
const body = JSON.parse(JSON.stringify(req.body));
const { tickers, type } = body;
console.log("stocks-api 74 | tickers length", tickers.length);
let stocksArray = [];
console.log("stocks-api.js 14 | body", body.tickers);
await tickers.forEach(async (ticker, index) => {
setTimeout(async () => {
const request = await fetch(
`https://www.alphavantage.co/query?function=${timePeriod(type)}&symbol=${ticker}&apikey=${process.env.ALPHA_VANTAGE_API_KEY}`
);
const data = await request.json();
stocksArray.push(Object.values(data));
console.log("stocks-api 84 | stocks array", stocksArray);
if (stocksArray.length === tickers.length) {
res.json({ tickers: stocksArray});
}
}, index * 12000);
});
});
app.listen(process.env.PORT || 8080, () => {
console.log("index.js 6 | server started...");
});