-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
44 lines (38 loc) · 1.23 KB
/
Copy pathserver.js
File metadata and controls
44 lines (38 loc) · 1.23 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
function toNaturalString(date) {
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var nat = monthNames[date.getMonth()];
nat += " " + date.getDate();
nat += ", " + date.getFullYear();
return nat;
}
function isValidDate(date) {
return ( (new Date(date) !== "Invalid Date" && !isNaN(new Date(date)) ));
}
var http = require('http')
var url = require('url')
var server = http.createServer(function(req, res) {
res.writeHead(200, { 'Content-Type': 'application/json' })
// default to null value
var result = JSON.stringify({ unix: null, natural: null})
try {
// get value from user (convert html spaces and other things)
var urlProps = url.parse(req.url, true)
var userVal = decodeURI(urlProps.pathname.slice(1))
// parse date
var d
if (isNaN(userVal)) {
d = new Date(userVal)
} else {
// number in ms so get ms
d = new Date(1000 * userVal)
}
if (isValidDate(d)) {
result = JSON.stringify({ unix: (d.getTime() / 1000),
natural: toNaturalString(d) })
}
} catch (err) {
console.log(err)
}
res.end(result)
})
server.listen(process.env.PORT)