/
app.js
138 lines (104 loc) · 3.47 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
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
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
var app = module.exports = express.createServer();
// Configuration
app.configure(function(){
//app.set('views', __dirname + '/views');
//app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// mongoose configuration
var mongoose = require('mongoose');
//local db
//mongoose.connect('mongodb://127.0.0.1/demo');
//remote db
mongoose.connect('mongodb://test:test@staff.mongohq.com:10081/senchaDemo');
//create the marker Model using the 'marker' collection as a data-source
markerModel = mongoose.model('marker', new mongoose.Schema({
markerID: String,
title: String,
lng: String,
lat: String,
icon: String,
description: String
}));
eventModel = mongoose.model('event', new mongoose.Schema({
id:String,
markerID: String,
title: String,
description: String,
day:String,
time:String,
trueVenue:String,
venue:String
}));
// Routes
//default url
app.get('/', function (req, res) {
res.redirect('/index.html');
res.end();
});
//get all markers
//http://localhost:5000/markers
app.get('/markers', function (req, res) {
// Allow Cross Domain Request from anywhere...
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
markerModel.find({}, function (err, markers) {
res.contentType('json');
res.write(req.query.callback+ '({"success":true,"data":' + JSON.stringify(markers)+ '})');
res.end();
});
});
// get all events
//http://localhost:5000/events/
app.get('/events', function(req, res) {
// Allow Cross Domain Request from anywhere...
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
eventModel.find({}, function (err,events) {
res.contentType('json');
res.write(req.query.callback+ '({"success":true,"data":' + JSON.stringify(events)+ '})');
res.end();
});
});
//get specific marker
//http://localhost:5000/marker/1234
/*app.get('/marker/:markerid', function (req, res) {
// Allow Cross Domain Request from anywhere...
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
console.log(req.params.markerid);
markerModel.find({markerID: +req.params.markerid}, function (err, markers) {
res.contentType('json');
res.write(req.query.callback+ '({"success":true,"data":' + JSON.stringify(markers)+ '})');
res.end();
});
});*/
//get events for marker id
//http://localhost:5000/events/1234
app.get('/events/:markerid', function(req, res) {
// Allow Cross Domain Request from anywhere...
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
eventModel.find({markerID: +req.params.markerid}, function (err,events) {
res.contentType('json');
res.write(req.query.callback+ '({"success":true,"data":' + JSON.stringify(events)+ '})');
res.end();
});
});
app.listen(process.env.PORT || 5000);
console.log('Running on Node.js Version: ' + process.version);
//console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);