-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
62 lines (56 loc) · 1.59 KB
/
Copy pathindex.js
File metadata and controls
62 lines (56 loc) · 1.59 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
const Koa = require('koa'),
app = new Koa(),
router = require('koa-router'),
fs = require('fs');
const routers = new router();
app.use(routers.routes());
routers.get('/', async(ctx) => {
ctx.type = 'html';
ctx.body = fs.createReadStream('./index.html');
});
routers.get('/api/timestamp', async(ctx) => {
ctx.type = "json";
let date = new Date();
ctx.body = {
"unix" : date.getTime() - date.getTimezoneOffset(),
"utc" : date.toUTCString()
};
});
routers.get('/api/timestamp/:datestring', async(ctx) => {
ctx.state.err = {
"error" : "Invalid Date"
};
ctx.type = 'json';
let string = ctx.params.datestring;
let not_valid = /[^0-9-]/;
if(not_valid.test(string))
{
ctx.body = ctx.state.err;
} else {
let not_unix_number = /[^\d]/;
if(not_unix_number.test(string))
{
let date = new Date(string+" UTC");
if(date.toString() === "Invalid Date")
ctx.body = ctx.state.err;
else
ctx.body = {
"unix" : date.getTime(),
"utc" : date.toUTCString()
};
}
else
{
let date = new Date(+string);
if(date.toString() === "Invalid Date")
ctx.body = ctx.state.err;
else
ctx.body = {
"unix" : +string,
"utc" : date.toUTCString()
};
}
}
});
const PORT = process.env.PORT || 8000;
app.listen(PORT, () => console.log("Server running on : ", PORT));