-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (39 loc) · 1.16 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
var express = require('express');
var morgan = require('morgan');
var app = express();
// Views config
app.set("views", "./views");
app.set("view engine", "pug");
// Middleware
app.use(morgan('combined'));
// Routes
app.get("/", function (req, res) {
res.render("index");
});
app.get("/:time", function(req, res) {
var queryTime = req.params.time;
var date, unixTime, stringTime;
var months = ["January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"];
if (isNaN(queryTime)) {
date = new Date(`${queryTime} GMT`);
if (isNaN(date)) {
unixTime = null;
stringTime = null;
} else {
unixTime = date.getTime()/1000;
stringTime = `${months[date.getUTCMonth()]} ${date.getUTCDate()}, ${date.getUTCFullYear()}`;
}
} else {
date = new Date(queryTime * 1000);
unixTime = date.getTime()/1000;
stringTime = `${months[date.getUTCMonth()]} ${date.getUTCDate()}, ${date.getUTCFullYear()}`;
}
var obj = {
unix: unixTime,
natural: stringTime
};
res.send(JSON.stringify(obj));
});
// Listen on port
app.listen(process.env.PORT || 5000);