-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
223 lines (167 loc) · 5.67 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*jshint node:true*/
var express = require('express');
var easypost = require ('easypost');
var mongo = require ('mongodb');
var monk = require ("monk");
var questionDao = require('./questionDao');
var answerDao = require('./answerDao');
var database = null;
var db = monk ("mongodb://IbmCloud_1140l45k_8qs5ds2q_qj7pb4ag:[email protected]:41190/IbmCloud_1140l45k_8qs5ds2q");
// setup middleware
var app = express();
app.use(app.router);
app.use(express.errorHandler());
app.use(express.static(__dirname + '/public')); //setup static public directory
app.set('view engine', 'ejs');
app.set('views', __dirname + '/views'); //optional since express defaults to CWD/views
app.get('/', function(req, res){
res.render('index.ejs');
});
app.get('/mobile_map', function(req, res){
res.render('mobile_map.ejs');
});
/**
Gets all the questions filtering by location
*/
app.get('/getQuestions', function(req, res){
console.log("getQuestions called, the query is:");
console.log(req.query);
questionDao.getQuestions(req.query, function(data){
console.log("Called getQuestions, the json result is: ");
console.log(data);
res.json({"res" : data});
});
});
app.get('/getLastQuestion', function(req, res){
console.log("getLastQuestion called, the query is:");
console.log(req.query);
questionDao.getLastQuestion(req.query, function(data){
console.log("Called getLastQuestion, the json result is: ");
console.log(data);
res.json({"res" : data});
});
});
/**
* handles the posted data from the clients
*/
app.post ("/putQuestion", function(req, res) {
console.log ("putQuestion called");
easypost.get (req, res, function(data) {
console.log ("printing the data received in post:");
console.log(JSON.parse(data));
questionData = JSON.parse(data);
if (questionData) {
questionDao.putQuestion(questionData, function(data){
console.log("Called putQuestion in Dao");
res.json({"res" : data});
});
}
});
});
app.post ("/putQuestionList", function(req, res) {
console.log ("putQuestionList called");
easypost.get (req, res, function(data) {
console.log ("printing the data received in post:");
console.log(JSON.parse(data));
questionListData = JSON.parse(data);
if (questionListData) {
questionListData.forEach(function(element, index, array){
questionDao.putQuestion(element, function(data){
console.log("Called putQuestion in Dao for" + data);
});
})
}
res.json({"res" : "success"});
});
});
app.get('/deleteQuestions', function(req, res){
console.log("deleteQuestions called, the query is:");
console.log(req.query);
questionDao.deleteQuestions(req.query, function(data){
console.log("Called deleteQuestions in DAO, the json result is: ");
console.log(data);
res.json({"res" : data});
});
});
/**
Gets all the answers filtering by location
*/
app.get('/getAnswers', function(req, res){
console.log("getAnswers called, the query is:");
console.log(req.query);
answerDao.getAnswers(req.query, function(data){
console.log("Called getAnswers, the json result is: ");
console.log(data);
res.json({"res" : data});
});
});
/** //TODO remove this, Kelson needed this to test his android application
Gets all the answers filtering by location
*/
app.post('/getAnswers', function(req, res){
console.log("getAnswers called with post, the query is:");
console.log(req.query);
answerDao.getAnswers(req.query, function(data){
console.log("Called getAnswers with post, the json result is: ");
console.log(data);
res.json({"res" : data});
});
});
/**
* handles the posted data from the clients
*/
app.post ("/putAnswer", function(req, res) {
console.log ("putAnswer called");
easypost.get (req, res, function(data) {
console.log ("printing the data received in post:");
console.log(JSON.parse(data));
answerData = JSON.parse(data);
if (answerData) {
answerDao.putAnswer(answerData, function(data){
console.log("Called putAnswer in Dao");
res.json({"res" : data});
});
}
});
});
app.get('/deleteAnswers', function(req, res){
console.log("deleteAnswers called, the query is:");
console.log(req.query);
answerDao.deleteAnswers(req.query, function(data){
console.log("Called deleteAnswers in DAO, the json result is: ");
console.log(data);
res.json({"res" : data});
});
});
app.post ("/putAnswerList", function(req, res) {
console.log ("putAnswerList called");
easypost.get (req, res, function(data) {
console.log ("printing the data received in post:");
console.log(JSON.parse(data));
answerListData = JSON.parse(data);
if (answerListData) {
answerListData.forEach(function(element, index, array){
answerDao.putAnswer(element, function(data){
console.log("Called putAnswer in Dao for" + data);
});
})
}
res.json({"res" : "success"});
});
});
//DO NOT TOUCH FROM HERE!
// There are many useful environment variables available in process.env.
// VCAP_APPLICATION contains useful information about a deployed application.
var appInfo = JSON.parse(process.env.VCAP_APPLICATION || "{}");
// TODO: Get application information and use it in your app.
// VCAP_SERVICES contains all the credentials of services bound to
// this application. For details of its content, please refer to
// the document or sample of each service.
var services = JSON.parse(process.env.VCAP_SERVICES || "{}");
// TODO: Get service credentials and communicate with bluemix services.
// The IP address of the Cloud Foundry DEA (Droplet Execution Agent) that hosts this application:
var host = (process.env.VCAP_APP_HOST || 'localhost');
var port = (process.env.VCAP_APP_PORT || 3000);
//app.listen(port, host);
app.listen(port);
console.log('App started on port ' + port);