-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.js
161 lines (141 loc) · 3.75 KB
/
web.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// MapTrip
var express = require("express");
var app = express();
app.use(express.logger('dev')); /* 'default', 'short', 'tiny', 'dev' */
app.use(express.bodyParser());
app.use(express.static(__dirname + '/public'));
var mongo = require('mongodb');
var mongoUri = process.env.MONGOLAB_URI ||
process.env.MONGOHQ_URL ||
'mongodb://localhost/mydb';
var db
mongo.Db.connect(mongoUri, function (err, mydb) {
if (!err) {
console.log("Connected to mongo");
db=mydb;
} else {
console.log("Not connected to mongo :(");
}
});
// Login
function requireLogin(req, res, next) {
if (req.session.loggedIn) {
next(); // allow the next route to run
} else {
// require the user to log in
res.redirect("/login"); // or render a form, etc.
}
}
// Automatically apply the `requireLogin` middleware
/*
app.all("/*", requireLogin, function(req, res, next) {
next();
});
*/
// Routes
app.get('/api/maps/:id', function(req, res) {
db.collection('maps', function(err, collection) {
collection.findOne({'_id':req.params.id}, function(err,doc) {
res.send(doc);
});
});
});
app.delete('/api/maps/:id', function(req, res) {
db.collection('maps', function(err, collection) {
collection.remove({'_id':req.params.id}, function(err,doc) {
res.json(true);
});
});
});
app.get('/api/maps', function(req, res) {
db.collection('maps', function(err, collection) {
collection.find({},['_id','title','description']).toArray(function(err,list) {
res.send(list);
});
});
});
app.post('/api/maps', function(req, res) {
var map=req.body;
db.collection('maps', function(err, collection) {
collection.save(map,{w:1},function(err,doc) {
res.send(map);
});
});
});
app.post('/api/maps/:id', function(req, res) {
var map=req.body;
db.collection('maps', function(err, collection) {
collection.save(map,{w:1},function(err,doc) {
res.send(map);
});
});
});
app.put('/api/maps', function(req, res) {
var map=req.body;
db.collection('maps', function(err, collection) {
collection.insert(map,{w:1},function(err,doc) {
res.send(doc);
});
});
});
app.get('/api/wipeout/', function(req, res) {
initDB();
res.send("DB initialitzed!");
});
var port = Number(process.env.PORT || 5000);
app.listen(port, function() {
console.log("Listening on " + port);
});
function initDB() {
maps= [
{
_id:"barcelona",title:"Barcelona",
description:"Viatge per Barcelona amb punts turístics per ordre",
sections: [
{
name:"Centre",
description:"Primer dia",
locations: [
{type:"point",coords:[41.40401, 2.17454],name:"Sagrada Familia",description:"On van tots els turistes"},
{type:"route",coords:[[41.3705, 2.1502],[41.40401, 2.17454]],name:"Linia recta",description:"Com no vagis volant..."},
{type:"point",coords:[41.3705, 2.1502],name:"Pavello mies van der rohe",description:"No se com s'escriu"}
]
},
{
name:"Montjuic",
description:"Segon dia",
locations: [
{type:"point",coords:[42.3705, 2.1502],name:"No se",description:"No se com s'escriu"}
]
}
]
},
{
_id:"londres",title:"London",
sections: [
{
locations: [
{type:"point",coords:{x:1,y:2},name:"Big Ben",description:"El rellotge gran"},
{type:"point",coords:{x:1,y:2},name:"British Museum",description:"La pedra roseta"}
]
}
]
},
{
_id:"bretanya",title:"Bretanya",
sections: [
{
locations:[]
}
]
},
];
db.collection('maps', function(er, collection) {
collection.drop(function (er,rs) {
console.log("Wiping out db..."+er+rs);
collection.insert(maps, {safe: true}, function(er,rs) {
console.log("Populating db..."+er+rs);
});
});
});
};