-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
66 lines (58 loc) · 1.77 KB
/
server.js
File metadata and controls
66 lines (58 loc) · 1.77 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
const cronJob = require('cron').CronJob;
const request = require('request');
const moment = require('moment');
const email = require('emailjs');
const http = require('http');
const appPort = Number(process.env.PORT || 2013);
const cronSchedule = process.env.CRON || '*/5 * * * *';
const pingUrls = [
"http://aptigram.apphb.com/background",
"http://simpletweetmap.apphb.com/",
"http://aptibot.herokuapp.com/"
];
const TIME_FORMAT_PATTERN = "YYYY-MM-DD, HH:mm:ss:SSS Z";
for (var urlIndex = 0; urlIndex < pingUrls.length; urlIndex++) {
const url = pingUrls[urlIndex];
setupPingerFor(url);
}
function setupPingerFor(url) {
try {
new cronJob(cronSchedule, function() {
request(url, function(error, response, scrapedForecasts) {
if (!error) {
log("Success pinging URL: " + url);
} else {
const errorMessage = "Error pinging URL: " + url + " Error: " + error;
log(errorMessage);
}
});
}, null, true);
log("Cron job started for URL: " + url);
} catch (ex) {
const errorMessage = "Cron pattern not valid: " + ex;
log(errorMessage);
}
}
function log(logMessage) {
return console.log(formatDate(moment()) + " " + logMessage);
}
function formatDate(moment) {
return moment.format(TIME_FORMAT_PATTERN);
}
const server = http.createServer(function(request, response) {
response.writeHead(200, {
"Content-Type": "text/html"
});
response.write("<!DOCTYPE html >");
response.write("<html>");
response.write("<head>");
response.write("<title>Hello World Page</title>");
response.write("</head>");
response.write("<body>");
response.write("Hello World!");
response.write("</body>");
response.write("</html>");
response.end();
});
server.listen(appPort);
console.log("Server is listening");