/
app.js
39 lines (33 loc) · 1.16 KB
/
app.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
//Basic required imports for NodeJS
var express = require('express');
var bodyParser = require('body-parser');
var cors = require('cors');
//Create an instance of express for our app and instantiate bodyParser and cors
var app = module.exports = express();
app.use(bodyParser.json());
app.use(cors());
//GET call to return JSON that formats natural and unix date
app.get('/date/:dateVal', function(req,res,next){
//Gets the request data for date
var dateVal = req.params.dateVal;
//Options for formatting date in natural date view
var dateFormattingOptions = {
year: 'numeric',
month: 'long',
day: 'numeric'
};
if(isNaN(dateVal)){
var naturalDate = new Date(dateVal);
naturalDate = naturalDate.toLocaleDateString("en-us", dateFormattingOptions);
var unixDate = new Date(dateVal).getTime()/1000;
}
else {
var unixDate = dateVal;
var naturalDate = new Date(dateVal * 1000);
naturalDate = naturalDate.toLocaleDateString("en-us", dateFormattingOptions);
}
res.json({unix: unixDate, natural: naturalDate});
});
app.listen(3000, function(){
console.log('Working!');
});