-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
96 lines (79 loc) · 2.73 KB
/
server.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
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
var express = require('express');
var partials = require('express-partials');
var bodyParser = require('body-parser');
var request = require('request');
var app = express();
var router = express.Router();
app.use(partials());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded( { extended: true }));
var fakeDB = {
'TSLA': {}
}
var controller = {};
controller.getInfo = function(req, res) {
if ( (req && req.headers) || (req && req.connection) ){
var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
console.log('is: ', ip);
}
//Checks fakedb for key before checking database
if (fakeDB['TSLA']['isHalted'] === undefined) {
console.log('api request starting')
request('http://www.nasdaqtrader.com/rss.aspx?feed=tradehalts', (err, response, body) => {
console.log('api request response')
if (err) {
// https error
console.log(err);
res.send(400, {data: 'error'})
} else {
// check for TSLA string in rss response
var containsTSLA = body.indexOf('TSLA') !== -1;
//Set the key/value pair in memory db so that its available going forward
fakeDB['TSLA']['isHalted'] = containsTSLA
fakeDB['TSLA']['nextRefresh'] = (new Date).getTime() + 30000
res.send(200, {data: containsTSLA})
}
});
} else {
var currentTime = (new Date).getTime();
if (currentTime < fakeDB['TSLA']['nextRefresh']) {
console.log('no api request')
res.send(200, {data: fakeDB['TSLA']['isHalted']})
} else {
console.log('api request starting')
request('http://www.nasdaqtrader.com/rss.aspx?feed=tradehalts', (err, response, body) => {
console.log('api request')
if (err) {
// https error
console.log(err);
res.send(400, {data: 'error'})
} else {
// check for TSLA string in rss response
var containsTSLA = body.indexOf('TSLA') !== -1;
//Set the key/value pair in memory db so that its available going forward
fakeDB['TSLA']['isHalted'] = containsTSLA
fakeDB['TSLA']['nextRefresh'] = (new Date).getTime() + 30000
res.send(200, {data: containsTSLA})
}
});
}
}
};
var routes = function(app) {
app.get('/info', controller.getInfo)
};
//default is a max-age of 0
app.use(express.static(__dirname+'/views'));
//max age of 60 seconds
// app.use(express.static(__dirname+'/client', {maxAge: 60000 }));
app.use('/api', router);
routes(router);
app.set('port', (process.env.PORT || 3000) );
app.listen(app.get('port'), function() {
console.log('server is running');
});
//keep uptime
setInterval(() => {
console.log('keeping up');
request.get("https://isteslahalted.glitch.me/");
}, 280000);