-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
96 lines (79 loc) · 2.65 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
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
var express = require('express');
var app = module.exports = express();
app.use(express.logger());
app.configure(function() {
app.use(express.errorHandler({
dumpExceptions: true,
showStack: true
}));
});
// Nearest resource
// Accept all methods
app.all('*', checkMethod, checkURL, paramExist, paramValue, function(req, res, next) {
next();
});
// Check method - only allow GET
function checkMethod(req, res, next) {
// If the method is GET then do something with it
if (req.method === 'GET') {
next();
}
// Any other method give correct HTTP code and informative JSON message
else {
res.json(405, {
'error': 'Method not allowed'
});
}
}
// Check URL is correct path
function checkURL(req, res, next) {
// If path is nearest then route on, otherwise redirect to Github repo
if (req.path == '/nearest') {
next();
}
else {
res.redirect(301, 'https://github.com/MichaelGordon/Height-API');
}
}
// Check location parameter exists
function paramExist(req, res, next) {
// If location query has been sent then return something
if (req.query.location) {
next();
}
// If location paramter is missing then give correct HTTP code and informative JSON message
else {
res.json(400, {
'error': 'Missing parameter - location'
});
}
}
// Parameter value checker function
function paramValue(req, res) {
// Store location query values and test against lat, lon regex
var location = req.query.location;
var reg = new RegExp ("^([-+]?\\d{1,2}([.]\\d+)?),\\s*([-+]?\\d{1,3}([.]\\d+)?)$");
if (reg.test(location)) {
getData(req, res, location);
}
// If location parameter value is not lat, long then give correct HTTP code and informative JSON message
else {
res.json(400, {
'error': 'Invalid coordinates for parameter location. Must be WGS84 latitude,longitude'
});
}
}
function getData(req, res, location) {
return res.json(200, {'location': '53.478112,0.108100'});
}
// Insert to db db.test.insert( {location: "53.478112,0.108100"} )
// Find with db.test.find({"location" : "53.478112,0.108100"})
// Allow app to be started up externally by test but if not start it in development mode
if (!module.parent) {
app.set('env', process.env.NODE_ENV || 'development');
app.set('port', process.env.PORT || 80);
var ip = process.env.IP;
app.listen(app.get('port'), process.env.IP || '0.0.0.0', function() {
console.log("Express server listening - http://%s:%d in %s mode", ip, app.get('port'), app.get('env') );
});
}