-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
157 lines (125 loc) · 5.66 KB
/
server.js
File metadata and controls
157 lines (125 loc) · 5.66 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var fs = require('fs');
var request = require('request');
var app = express();
app.set('port', (process.env.PORT || 9890));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'pub')));
// #region Configure the Stock Quote Retrieval stuff
require('./src/extensionMethods.js');
var getDateString = function (dateVal) {
dateVal = dateVal || new Date();
var yyyy = dateVal.getFullYear() + '';
var MM = (dateVal.getMonth() + 1) + '';
var dd = (dateVal.getDate() + 1) + '';
if (MM.length < 2)
MM = '0' + MM;
if (dd.length < 2)
dd = '0' + dd;
return (yyyy + '-' + MM + '-' + dd);
};
var getDaysAgo = function (numDays) {
var a = new Date();
a.setDate(a.getDate() - numDays);
return a;
}
var yahooFinanceApiUri = 'https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.historicaldata%20where%20symbol%20in%20%28{0}%29%20and%20startDate%20%3D%20%22{1}%22%20and%20endDate%20%3D%20%22{2}%22&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=';
var getStockData = function (symbols, daysHistory, callback) {
if (typeof (symbols) === 'string') {
symbols = [symbols];
}
var symbolsString = '%22';
if (Array.isArray(symbols)) {
symbolsString +=(symbols.join('%22%2C%22') + '%22');
}
var endDateString = getDateString();
var startDateString = getDateString(getDaysAgo(daysHistory));
var actualFinanceApiUri = String.format(yahooFinanceApiUri, symbolsString, startDateString, endDateString);
console.log('Stock API request URI: ' + actualFinanceApiUri);
request(actualFinanceApiUri, function (error, response, body) {
var err;
if (error) {
err = new Error('An error has occurred while calling the Yahoo Finance API: ' + error);
}
else if (response.statusCode !== 200) {
err = new Error('A non-success response (' + response.statusCode + ') was returned from the Yahoo Finance API: ' + body);
}
if (response.statusCode == 200) {
callback(err, body);
}
})
};
var handleApiRequest = function (req, res, next) {
getStockData(req.stockSymbols, req.daysHistory, function (err, result) {
if (err) {
next(err);
return;
}
res.status = 200;
res.write(result);
res.end();
});
};
// #endregion Configure the Stock Quote Retrieval Stuff
// #region Set Up the router
// create the router
var apiRouter = express.Router();
// define a parameter for scope of data requested
apiRouter.param('daysHistory', function (req, res, next, daysHistoryVal) {
var numDays = Number(daysHistoryVal);
Object.defineProperty(req, 'daysHistory', { enumerable: true, value: isNaN(numDays) ? 30 : numDays });
next();
});
// define a parameter for the stock symbols selected
apiRouter.param('stockSymbols', function (req, res, next, stockSymbolsVal) {
var symbols = stockSymbolsVal.split(",");
Object.defineProperty(req, 'stockSymbols', { enumerable: true, value: symbols || ["GOOG"] });
next();
});
apiRouter.route('/stock-history/:daysHistory')
.all(function (req, res, next) { next(); })
.post(function (req, res, next) { next(new Error('This action is not available for the "POST" HTTP verb.')); })
.put(function (req, res, next) { next(new Error('This action is not available for the "PUT" HTTP verb.')); })
.delete(function (req, res, next) { next(new Error('This action is not available for the "DELETE" HTTP verb.')); })
.get(function (req, res, next) {
// Do the work...
// Here's where we should define the stock symbols since they are not supplied in the request...but that should really be in a POST request with a body. We'll handle that later.
handleApiRequest(req, res, next);
});
apiRouter.route('/stock-history/:stockSymbols/:daysHistory')
.all(function (req, res, next) { next(); })
.post(function (req, res, next) { next(new Error('This action is not available for the "POST" HTTP verb.')); })
.put(function (req, res, next) { next(new Error('This action is not available for the "PUT" HTTP verb.')); })
.delete(function (req, res, next) { next(new Error('This action is not available for the "DELETE" HTTP verb.')); })
.get(function (req, res, next) {
// Do the work...
// Here's where we should define the stock symbols since they are not supplied in the request...but that should really be in a POST request with a body. We'll handle that later.
handleApiRequest(req, res, next);
});
apiRouter.route('/stock-history')
.all(function (req, res, next) { next(); })
.post(function (req, res, next) { next(new Error('This action is not available for the "POST" HTTP verb.')); })
.put(function (req, res, next) { next(new Error('This action is not available for the "PUT" HTTP verb.')); })
.delete(function (req, res, next) { next(new Error('This action is not available for the "DELETE" HTTP verb.')); })
.get(function (req, res, next) {
// Do the work...
Object.defineProperty(req, 'daysHistory', { enumerable: true, value: 5 });
Object.defineProperty(req, 'stockSymbols', { enumerable: true, value: ["GOOG"] });
handleApiRequest(req, res, next);
});
app.use('/api', apiRouter);
// #endregion Set Up the router
//app.get('/stock-ticker', function (req, res) {
// // This is our reverse proxy to the web API for stock ticker data...
//});
// catch 404 and forward to error handler
app.use(function (req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
var server = app.listen(app.get('port'), function () {
console.log('Express server listening on port ' + server.address().port);
});