-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (39 loc) · 1.3 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
const express= require('express');
const path=require('path');
require('dotenv').config();
const port =process.env.PORT || 8000;
const request = require('request');
const app = express();
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.use(express.static(path.join(__dirname,"assets")))
app.set('view engine','ejs')
app.get('/', (req,res)=>{
res.render('home',{marketPrice: null,price:null});
})
app.post('/submit', (req,res)=>{
const {symbol,time} = req.body;
console.log(symbol,time);
const url = `https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=${symbol}&interval=${time}min&apikey=${process.env.KEY}`;
var marketPrice=0,price=0;
request.get({
url: url,
json: true,
headers: {'User-Agent': 'request'}
}, (err, response, data) => {
if (err) {
console.log('Error:', err);
} else if (response.statusCode !== 200) {
console.log('Status:', response.statusCode);
} else {
const resData = data[`Time Series (${time}min)`]
marketPrice=resData[Object.keys(resData)[0]]['1. open'];
price=resData[Object.keys(resData)[1]]['1. open'];
// console.log(marketPrice, price);
res.render('home',{marketPrice: marketPrice,price:price});
}
});
})
app.listen(port,()=>{
console.log('listening on port',port);
})