-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
96 lines (82 loc) · 2.73 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
/*
* @author: Thomas Stockx
* @copyright: OKFN Belgium
*
* Node.js entry point
*/
// declare external files
var express = require("express");
var utils = require("./utils");
var users = require("./urlroutes/users");
var spots = require("./urlroutes/spots");
var routes = require("./urlroutes/routes");
var config = require("./auth/dbconfig.js");
// use express and its bodyParser for POST requests.
var app = express();
app.use(express.bodyParser());
// prevent server death in case of uncaught exceptions
process.on('uncaughtException', function (exception) {
console.log(exception);
});
/**
* Our hosting service provides database information in the VCAP_SERVICES environment variable.
* If it does not exist, we'll connect to a localhost MongoDB.
*/
if (process.env.VCAP_SERVICES) {
var env = JSON.parse(process.env.VCAP_SERVICES);
var mongo = env['mongodb-1.8'][0]['credentials'];
}
else {
var mongo = {
"hostname": "localhost",
"port": 27017,
"username": "",
"password": "",
"name": "",
"db": config.dbname
}
}
/**
* Building the URL to the MongoDB.
*/
var generate_mongo_url = function (obj) {
obj.hostname = (obj.hostname || 'localhost');
obj.port = (obj.port || 27017);
obj.db = (obj.db || 'test');
if (obj.username && obj.password) {
return "mongodb://" + obj.username + ":" + obj.password + "@" + obj.hostname + ":" + obj.port + "/" + obj.db;
}
else {
return "mongodb://" + obj.hostname + ":" + obj.port + "/" + obj.db;
}
}
var mongourl = generate_mongo_url(mongo);
exports.mongourl = mongourl;
/**
* Fix cross-domain requests errors, this should probably be cleaned up before a real release.
*/
app.all('/*', function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Methods", "GET,POST");
next();
});
// define the users API url routes.
app.get("/users/login/:base64", users.login);
app.get("/users/logout/:token", users.logout);
app.get("/users/:key", users.dropAll);
// define the spots API url routes.
app.get("/spots", spots.findSpotsByLatLong);
app.get("/spots/checkin", spots.checkIn);
app.get("/spots/relevant", spots.findRelevantSpots);
app.get("/spots/search", spots.search);
app.get("/spots/:id", spots.findById);
// define the routes API url routes.
app.get("/routes", routes.findRoutesStartingAtSpot);
app.post("/routes", routes.addRoute);
app.get("/routes/generate/:channelname", routes.generateRoute);
app.get("/routes/generate", routes.generateRouteFromChannelArray);
app.get("/routes/:id", routes.findById);
// start server on port 1337
console.log("Listening on port 1337...");
app.listen(1337);