-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
48 lines (44 loc) · 1.22 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
'use strict';
var express = require('express');
var routes = require('./app/routes/index.js');
var mongoose = require('mongoose');
var passport = require('passport');
var session = require('express-session');
var path=require('path');
var app = express();
app.use('/', function (req,res,next)
{
var getpath=decodeURIComponent(req.path).replace(/(^\/)/g, "");//remove leading / and decode URI
if (getpath=='') //by default return the help page
{
res.sendFile(path.join(__dirname,'public/indexTimeMicroservice.html'));
}
else // we got a time request so need to return the json
{
//check if date can be parsed
var d=Date.parse(getpath);
if (d)
{
//console.log(d);
//console.log(new Date(d).getFullYear());
var jsn={"unix":d/1000,"natural":new Date(d).toDateString()};
res.send(jsn);
}
else
{
//check if valid unix timestamp
var d=getpath*1000;
var valid = (new Date(d)).getTime() > 0;
if (valid)
var jsn={"unix":d/1000,"natural":new Date(d).toDateString()};
else
var jsn={"unix":null,"natural":null};
res.send(jsn);
//console.log(getpath);
}
}
});
var port = process.env.PORT || 8080;
app.listen(port, function () {
console.log('Node.js listening on port ' + port + '...');
});