-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
77 lines (60 loc) · 1.96 KB
/
index.js
File metadata and controls
77 lines (60 loc) · 1.96 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// API Web App by Binyomin Mansheim
const express = require('express');
const app = express();
const exphbs = require('express-handlebars');
const request = require('request');
const bodyParser = require('body-parser')
const path = require('path');
const fs = require('fs');
// Generate random ticker symbol
const stocksFileContents = fs.readFileSync(path.join(__dirname, 'stocks.txt')).toString();
const stocks = [];
stocksFileContents.split('\n').forEach((line) => {
stocks.push({
'tickerSymbol': line
})
});
const randomStock = Math.floor(Math.random() * stocks.length);
var ticker = stocks[randomStock].tickerSymbol
const PORT = process.env.PORT || 5000;
// use body parser middleware
app.use(bodyParser.urlencoded({ extended: false }));
// API key pk_6b35f7e7fb0842ada1248b416042ae8f
// create call API function
function call_api(finishedAPI, ticker) {
request('https://cloud.iexapis.com/stable/stock/' + ticker + '/quote?token=pk_6b35f7e7fb0842ada1248b416042ae8f', { json: true }, (err, res, body) => {
if (err) { return console.log(err); }
if (res.statusCode === 200) {
finishedAPI(body);
};
});
}
// Set Handlebars Middleware
app.engine('handlebars', exphbs());
app.set('view engine', 'handlebars');
const otherstuff = "This is other stuff...";
// Set handlebar index GET route
app.get('/', function (req, res) {
call_api(function (doneAPI) {
res.render('home', {
stock: doneAPI
});
}, ticker);
});
// Set handlebar index POST route
app.post('/', function (req, res) {
call_api(function (doneAPI) {
//ticker_symbol = req.body.stock_ticker;
res.render('home', {
stock: doneAPI,
});
}, req.body.stock_ticker);
});
app.get('/about.html', function (req, res) {
res.render('about');
});
// Set static folder
app.use(express.static(path.join(__dirname, 'public')));
app.listen(PORT, () => {
console.log('Server listening on port ' + PORT)
})