-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
72 lines (67 loc) · 2.13 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
var express = require('express');
var app = express();
function month (m){
switch (m){
case 0: return "January";
case 1: return "February";
case 2: return "March";
case 3: return "April";
case 4: return "May";
case 5: return "June";
case 6: return "July";
case 7: return "August";
case 8: return "September";
case 9: return "October";
case 10: return "November";
case 11: return "December";
}
}
function checkDate(d){
if (Object.prototype.toString.call(d) === "[object Date]"){
if (isNaN(d.getTime())) {
return false
} else {
return true
}
} else {
return false
}
}
app.get('/', function(req,res){
res.sendFile(__dirname + '/index.html');
})
app.get('*', function(req, res){
// if (req.url == '/'){
// res.sendFile(__dirname + '/index.html');
// }
var request = req.url.slice(1, req.url.length);
if (!isNaN(request)){
var date = new Date(request*1000);
}
if (date){
//we have unix date and can process it
var response = {
'unix': request,
'natural': month(date.getMonth()) + ' ' + date.getDate() + ', ' + date.getFullYear()
}
res.send(response);
}
request = request.split('%20').join(' ');
//var date = request.match(/January|February|March|April|May|June|July|August|Septemper|October|November|December\s\d{2}[,]\s\d{4}/);
if (request.match(/January|February|March|April|May|June|July|August|Septemper|October|November|December\s\d{2}[,]\s\d{4}/)) {
//we have natural date
var date = new Date(request);
if (checkDate(date) && date.getDate() == request.split(' ')[1].substring(0,2)) {
var response = {
'unix' : ((new Date(request)).getTime()/1000).toString(),
'natural': request
}
res.send(response);
} else res.send('Not valid date!');
} else {
res.send('Not valid date!');
}
})
app.listen(8080, function(){
console.log('app listening on port 8080');
});