-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
42 lines (36 loc) · 1.31 KB
/
app.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
require('dotenv').config();
const express = require('express');
const app = express();
const { fetchAPI, wait, validateDate, updatePrices } = require('./functions');
const serverURL = process.env.serverURL;
app.get('/stocks/:page?', async (req, res) => {
const url = `${serverURL}/stocks/${req.params.page || 1}`;
await wait(250).then(async() => {
const response = await fetchAPI(url);
res.send(response);
});
});
app.get('/stock/quote', async(req, res) => {
const { symbol, startDate, endDate } = req.query;
let response = {};
if (symbol && validateDate(startDate) && validateDate(endDate)) {
const url = `${serverURL}/stock/quote?symbol=${symbol}&startDate=${startDate}&endDate=${endDate}`;
await wait(250).then(async() => {
response = await fetchAPI(url);
});
}else {
response = { error: 'Invalid query string parameter' }
}
res.json(response);
});
app.get('/stock/:symbol', async(req, res) => {
const url = `${serverURL}/stock/${req.params.symbol}`;
await wait(250).then(async() => {
const response = await fetchAPI(url);
res.send(response);
});
});
app.get('/', async(req, res) => {
return res.redirect('https://github.com/algermakiputin/PSEStocksAPI');
})
app.listen(process.env.PORT);