-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
50 lines (42 loc) · 1.04 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
// server.js
// where your node app starts
// init project
var express = require('express');
var app = express();
var moment = require('moment');
var path = require('path');
app.get('/:datestring', function(req,res) {
var myDate;
if(/^\d{8,}$/.test(req.params.datestring)) {
myDate = moment(req.params.datestring, "X");
} else {
myDate = moment(req.params.datestring, "MMMM D, YYYY");
}
if(myDate.isValid()) {
res.json({
unix: myDate.format("X"),
natural: myDate.format("MMMM D, YYYY")
});
} else {
res.json({
unix: null,
natural: null
});
}
});
app.get('/', function(req, res) {
var fileName = path.join(__dirname, '/views/index.html');
res.sendFile(fileName, function (err) {
if (err) {
console.log(err);
res.status(err.status).end();
}
else {
console.log('Sent:', fileName);
}
});
});
// listen for requests :)
var listener = app.listen(process.env.PORT, function () {
console.log('Your app is listening on port ' + listener.address().port);
});