-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
69 lines (50 loc) · 1.6 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
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
// Stock Market Portfolio App By John Elder Codemy.com
const express = require('express');
const app = express();
const exphbs = require('express-handlebars');
const path = require('path');
const request = require('request');
const bodyParser = require('body-parser');
const PORT = process.env.PORT || 5000;
// use body parser middleware
app.use(bodyParser.urlencoded({extended: false}));
// API KEY pk_062031d20883444f9ea74e2610fe2011
// create call_api function
function call_api(finishedAPI, ticker) {
request('https://cloud.iexapis.com/stable/stock/' + ticker + '/quote?token=pk_062031d20883444f9ea74e2610fe2011', { json: true }, (err, res, body) => {
if (err) {return console.log(err);}
if (res.statusCode === 200){
//console.log(body);
finishedAPI(body);
};
});
};
// Set Handlebars Middleware
app.engine('handlebars', exphbs());
app.set('view engine', 'handlebars');
const otherstuff = "hello there, this is other stuff!";
// Set handlebar index GET route
app.get('/', function (req, res) {
call_api(function(doneAPI) {
res.render('home', {
stock: doneAPI,
});
}, "fb");
});
// Set handlebar index POST route
app.post('/', function (req, res) {
call_api(function(doneAPI) {
//console.log(doneAPI);
//posted_stuff = req.body.stock_ticker;
res.render('home', {
stock: doneAPI,
});
}, req.body.stock_ticker);
});
// create about page route
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));