-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
44 lines (37 loc) · 1.64 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
const NodeCache = require('node-cache');
const express = require('express');
const app = express();
const tickerCache = new NodeCache({ stdTTL: 40000, checkperiod: 10000 });
const PORT = process.env.PORT || 8002;
const getTickers = async () => {
const NASDAQ = await fetch('https://raw.githubusercontent.com/khant14/stock-symbols/main/nasdaq.json').then(response => response.json());
const NYSE = await fetch('https://raw.githubusercontent.com/khant14/stock-symbols/main/nyse.json').then(response => response.json());
const AMEX = await fetch('https://raw.githubusercontent.com/khant14/stock-symbols/main/amex.json').then(response => response.json());
const tickers = new Map();
const storeTickers = (exchange) => {
exchange.forEach(ticker => {
tickers.set(ticker.symbol, ticker.name);
});
}
storeTickers(NASDAQ);
storeTickers(NYSE);
storeTickers(AMEX);
tickerCache.set('tickers', tickers);
return tickers;
}
(async () => {
let tickers = await getTickers();
tickerCache.on("expired", async () => {
tickers = await getTickers();
});
app.get('/:ticker', (req, res) => {
const searchTicker = req.params.ticker;
const resultSymbolsMatch = Array.from(tickers.keys()).sort().filter(element => element.includes(searchTicker));
const resultNameMatch = Array.from(tickers.values()).sort().filter(element => element.toLowerCase().includes(searchTicker.toLowerCase()));
const combinedResult = [...resultSymbolsMatch, ...resultNameMatch].map(element => ({ ticker: element, name: tickers.get(element) }));
res.send(combinedResult);
});
app.listen(PORT, () =>
console.log(`Listening on port: ${PORT}!`),
);
})();