-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
84 lines (68 loc) · 2.41 KB
/
app.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
var port = process.env.PORT || 3000,
http = require('http'),
https = require('https'),
fs = require('fs'),
url = require('url'),
path = require('path');
var doTheStupidUpdate = require('./update');
var log = function(entry) {
fs.appendFileSync('/tmp/sample-app.log', new Date().toISOString() + ' - ' + entry + '\n');
};
var server = http.createServer(function (req, res) {
if (req.method === 'POST') {
var body = '';
req.on('data', function(chunk) {
body += chunk.toString('utf8');
});
req.on('end', function() {
if (req.url === '/') {
log('Received message: ' + body);
} else if (req.url == '/manualUpdate') {
doTheStupidUpdate();
log('Did manual update');
}
res.writeHead(200, 'OK', {'Content-Type': 'text/plain'});
res.end();
});
} else {
let parsed = url.parse(req.url,true); // true makes it an object
//console.log(parsed.query);
let uri = parsed.pathname;
let filename = path.join(process.cwd(), 'public', uri);
fs.exists(filename, function(exists) {
if (!exists) {
res.writeHead(404, {'Content-Type': 'text/plain'});
res.write("404 Not Found\n");
res.end();
return;
}
if (fs.statSync(filename).isDirectory()) filename += 'index.html';
fs.readFile(filename, 'binary', function(err, file) {
if (err) {
res.writeHead(500, {"Content-Type": "text/plain"});
res.write(err + "\n");
res.end();
return;
}
res.writeHead(200);
res.write(file, "binary");
res.end();
})
})
/*res.writeHead(200);
res.write(html);
res.end();*/
}
});
// Listen on port 3000, IP defaults to 127.0.0.1
server.listen(port);
let schedule = require('node-schedule');
let testSchedule = '*/1 * * * *'; // every minute
let hourlySchedule = '0 */1 * * *'; // every hour
let dailySchedule = '0 2 */1 * *'; // every day at 2am
schedule.scheduleJob(dailySchedule, function(){
console.log(new Date(), 'Updating data!');
doTheStupidUpdate();
});
console.log('Server running at http://127.0.0.1:' + port + '/');
console.log(__dirname);