-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
107 lines (85 loc) · 2.58 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/******************************************************
* PLEASE DO NOT EDIT THIS FILE
* the verification process may break
* ***************************************************/
'use strict';
var fs = require('fs');
var moment = require('moment');
var express = require('express');
var app = express();
if (!process.env.DISABLE_XORIGIN) {
app.use(function(req, res, next) {
var allowedOrigins = ['https://narrow-plane.gomix.me', 'https://www.freecodecamp.com'];
var origin = req.headers.origin || '*';
if(!process.env.XORIG_RESTRICT || allowedOrigins.indexOf(origin) > -1){
console.log(origin);
res.setHeader('Access-Control-Allow-Origin', origin);
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
}
next();
});
}
app.use('/public', express.static(process.cwd() + '/public'));
app.route('/_api/package.json')
.get(function(req, res, next) {
console.log('requested');
fs.readFile(__dirname + '/package.json', function(err, data) {
if(err) return next(err);
res.type('txt').send(data.toString());
});
});
app.route('/')
.get((req, res) => {
let params = req.query;
console.log(params);
return res.json(params);
// res.sendFile(process.cwd() + '/views/index.html');
})
app.get("/:myVariable", (req, res) => {
console.log(req.params);
let paramOne = req.params.myVariable;
// get rid of special symbols
paramOne = decodeURI(paramOne); // .replace("%20", " ");
let isItADate = false;
// try to convert to a Date.
let theDate = new Date(paramOne);
if (!isNaN(theDate.getTime())) {
isItADate = true;
} else {
theDate = new Date(paramOne * 1000);
if (!isNaN(theDate.getTime())) {
isItADate = true;
}
}
if (isItADate) {
// create the date
let dateForHumans = moment(theDate).format('MMMM Do YYYY, h:mm:ss a');
let dateEpoch = theDate.getTime() / 1000;
let dateObjectReturned = {
"unix": dateEpoch,
"natural": dateForHumans
}
res.json(dateObjectReturned);
} else {
// respond with saying no
res.send('no way!');
}
});
app.route('*').get((req, res) => {
});
// Respond not found to all the wrong routes
app.use((req, res, next) => {
res.status(404);
res.type('txt').send('Not found');
});
// Error Middleware
app.use((err, req, res, next) => {
if(err) {
res.status(err.status || 500)
.type('txt')
.send(err.message || 'SERVER ERROR');
}
})
app.listen(process.env.PORT, function () {
console.log('Node.js listening ...');
});