-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
67 lines (43 loc) · 1.27 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
const express = require('express');
const app = express();
const cors = require('cors');
app.use(cors({optionsSuccessStatus: 200})); // some legacy browsers choke on 204
app.use(express.static('public'));
// Routing
app.get("/", function(req, res) {
res.sendFile(`${__dirname}/views/index.html`);
});
app.get("/api/easteregg", function(req, res) {
res.json({greeting: "Oh, you've found this! Well, congrats! :p"});
});
app.get("/api/timestamp", function(req, res) {
const date = new Date();
res.json({
"unix": date.valueOf(),
"utc": date.toUTCString()
});
});
app.get("/api/timestamp/:dateParam", function(req, res) {
let dateParam = req.params.dateParam;
if (/^\d{5,}$/.test(dateParam))
dateParam = parseInt(dateParam);
const date = new Date(dateParam);
if (date.toString() == "Invalid Date") {
res.json({
"error": "Invalid Date"
});
} else {
res.json({
"unix": date.valueOf(),
"utc": date.toUTCString()
});
}
});
// No matching route
app.use(function(req, res, next) {
res.status(404).sendFile(`${__dirname}/views/404.html`);
});
// Listening for requests
const listener = app.listen(process.env.PORT, function () {
console.log('Your app is listening on port ' + listener.address().port);
});