-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
53 lines (42 loc) · 1.55 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
// * IMPORTS
const dotenv = require("dotenv").config();
const express = require("express");
const app = express();
// * https://en.wikipedia.org/wiki/Cross-origin_resource_sharing
const cors = require('cors');
app.use(cors({ optionsSuccessStatus: 200 }));
// * http://expressjs.com/en/starter/static-files.html
app.use(express.static('public'));
// * http://expressjs.com/en/starter/basic-routing.html
app.get("/", function(req, res) {
res.sendFile(__dirname + '/views/index.html');
});
// * API ROUTES
app.get("/api/", (req, res) => {
// returns current date in unix/utc format
const time = new Date();
res.json({
unix: new Date(time).getTime(),
utc: new Date(time).toUTCString()
});
});
app.get("/api/:date?", (req, res) => {
// returns given date in unix/utc format
let date_string = req.params.date;
if (isNaN(new Date(date_string))) { // checks if date_string is a valid date(yyyy-mm-dd)
if (isNaN(new Date(parseInt(date_string)))) { // checks if date_string is a valid date(unix)
return res.status(400).json({ error: "Invalid Date" })
} else {
date_string = parseInt(date_string); // the recived param is a string so it needs to parsed do int when unix value is sent
}
}
return res.status(200).json({
unix: new Date(date_string).getTime(),
utc: new Date(date_string).toUTCString()
});
});
// * STARTS EXPRESS SERVER
const PORT = process.env.PORT || 3000;
app.listen(PORT, function() {
console.log(`App listening at http://localhost:${PORT}`);
});