-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.js
More file actions
60 lines (46 loc) · 1.73 KB
/
Copy pathServer.js
File metadata and controls
60 lines (46 loc) · 1.73 KB
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
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
// Allowing our app to use the body parser package.
app.use(bodyParser.urlencoded({extended:false}))
var axios = require("axios").default;
var options = {
method: 'GET',
url: 'https://latest-stock-price.p.rapidapi.com/price',
params: {Indices: 'NIFTY 50'},
headers: {
'X-RapidAPI-Key': '1037d502admshb191f3fc255459bp16a3e7jsnbcf32976778d',
'X-RapidAPI-Host': 'latest-stock-price.p.rapidapi.com'
}
};
app.get("/", function(req, res) {
res.sendFile(__dirname + "/index.html");
});
// HANDLING THE POST REQUEST ON /DATA ROUTE.
app.post("/data", function(req, res) {
var itemSelectedFromDropdown = req.body.stockSelected;
axios.request(options).then(function (response) {
var dataFromResponse = response.data;
for(var i = 0; i<dataFromResponse.length; i++){
if(dataFromResponse[i].symbol == itemSelectedFromDropdown){
var dataOfStock = dataFromResponse[i];
res.send("<html><body> <h1><strong> " + dataOfStock.symbol + "</strong></h1>"+
"<h1> Open: " + dataOfStock.open + "</h1>" +
"<h1> Day High: "+ dataOfStock.dayHigh + "</h1>" +
"<h1> Day Low: "+ dataOfStock.dayLow + "</h1>" +
"<h1> Last Price: "+ dataOfStock.lastPrice + "</h1>" +
"<h1> Previous Close: "+ dataOfStock.previousClose + "</h1>" +
"<h1> Year Low: "+ dataOfStock.yearHigh + "</h1>" +
"<h1> Year Low: "+ dataOfStock.yearLow + "</h1>" +
"<h1> Last Update Time: "+ dataOfStock.lastUpdateTime + "</h1>" +
"</body></html>")
}
}
}).catch(function (error) {
console.error(error)
});
});
var port = 3000;
app.listen(port, function() {
console.log("Server started successfully at port 3000!");
});