-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.js
More file actions
48 lines (44 loc) · 1.46 KB
/
program.js
File metadata and controls
48 lines (44 loc) · 1.46 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
// server.js
// where your node app starts
// init project
var express = require('express');
var app = express();
var monthNames = [
"January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December"
];
app.get('/:date', function(req, res) {
console.log(req.params.date);
if(String(new Date(Number(req.params.date)*1000)) != 'Invalid Date') {
try{
var date = new Date(Number(req.params.date)*1000);
console.log(date);
var day = date.getDate();
var monthIndex = date.getMonth();
var year = date.getFullYear();
res.send(JSON.stringify({unix: req.params.date, natural: monthNames[monthIndex] + ' ' + day + ', '+ year}));
} catch(err) {
console.log(err);
res.send(JSON.stringify({unix: null, natural: null}))
}
} else if(String(new Date(req.params.date)) != 'Invalid Date') {
try {
var date = new Date(req.params.date);
res.send(JSON.stringify({unix: Date.parse(date)/1000, natural: req.params.date}));
} catch(err) {
console.log(err);
res.send(JSON.stringify({unix: null, natural: null}));
}
} else {
res.send(JSON.stringify({unix: null, natural: null}));
}
});
app.get('/', function(req, res){
res.send(JSON.stringify({unix: null, natural: null}));
});
// listen for requests :)
var listener = app.listen(process.env.PORT, function () {
console.log('Your app is listening on port ' + listener.address().port);
});