-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
200 lines (177 loc) · 5 KB
/
server.js
File metadata and controls
200 lines (177 loc) · 5 KB
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
//=================
// SETUP
// ================
//IMPORT MODULES
var http = require('http'); // create server with http module so that we can https later
var express = require('express'); //handle routing and server details
var bodyParser = require('body-parser'); //body paring middleware
var Q = require('q');
var config = require('./config/config.local.js');
var db_conf = config.db;
var db;
//const MongoClient = require('mongodb').MongoClient;
const mongoose = require('mongoose');
var debug = config.debug;
var app = express();
var port = 3000;
app.use(bodyParser.json({limit: '5mb'}));
app.use(bodyParser.urlencoded({extended : true}));
//BEGIN
addRoutes()
.then(function(){
startServer()
})
.catch(function(ex){
console.log('ERROR');
console.log(ex);
})
function addRoutes(callback){
var deferred = Q.defer();
require('./app/routes/account.routes.js')(app)
require('./app/routes/key.routes.js')(app);
deferred.resolve(console.log("Added Routes"));
deferred.promise.nodeify(callback);
return deferred.promise;
}
/**
* Returns mongoose connection object
*/
function connectMongo(callback){
var deferred = Q.defer();
mongoose.connect(db_conf.url)
.then(function(mongoose){
var connection = mongoose.connection;
console.log("Connected to MongoDb");
deferred.resolve(connection);
})
.catch(function(ex){
console.log("Unable to connect to mongo. Terminating.")
deferred.reject(ex);
});
deferred.promise.nodeify(callback);
return deferred.promise;
}
/**
* Start our HTTP server, begin listening
* on the configured port,
* then connect to our mongo instance with mongoose
*/
function startServer(callback){
var deferred = Q.defer();
http.createServer(app).listen(port, function(){
connectMongo()
.then(function(db){
console.log("Server listening on port: "+port);
deferred.resolve(db);
})
.catch(function(ex){
console.log(ex);
process.exit();
});
});
deferred.promise.nodeify(callback);
return deferred.promise;
}
/*==============================
========DEPRECATED==============
================================*/
/**
* This was the original way I was connection
* to my mongodb as I was refamiliarizing myself with mongo
*/
function oldServerConfig(){
app.get('/', function(req, res){
res.json({"message": "Ready."});
});
MongoClient.connect(db_conf.url)
.then(function(client){
db = client.db('app');
http.createServer(app).listen(port, function(){
console.log("Server on port: "+port);
if(debug){
tests()
.then(function(res){
console.log("Tests completed: Success");
})
.catch(function(exception){
console.log("Tests completed: Failure");
});
}
});
})
.catch(function(exception){
if(exception.name == 'MongoNetworkError'){
console.log("Are you sure mongodb is running?");
}
console.log(exception);
process.exit();
});
}
/**
* Quick function to test that mongo is connected
* and it prints one of the documents saved
*/
function testReadMongo(lookup,callback){
var deferred = Q.defer();
db.collection('users').find(lookup).toArray()
.then(function(res){
console.log("find from database");
deferred.resolve(res);
})
.catch(function(exception){
console.log(exception);
deferred.reject(exception);
})
deferred.promise.nodeify(callback);
return deferred.promise;
}
/**
* Simple test to insertOne into Mongodb
* @param {data to write} testdata
* @param {*} callback
*/
function testWriteMongo(testdata,callback){
var deferred = Q.defer();
db.collection('users').insertOne(testdata)
.then(function(res){
console.log("insertOne to database");
deferred.resolve(res);
})
.catch(function(exception){
console.log(exception);
deferred.reject(exception);
});
deferred.promise.nodeify(callback);
return deferred.promise;
}
/**
* Test function
* @param {} callback
*/
function tests(callback){
var deferred = Q.defer();
if(debug){
testWriteMongo(testdata)
.then(function(res){
var inner_deferred = Q.defer();
console.log("Successfully wrote to db");
testReadMongo(testdata)
.then(function(res){
console.log("Sucessfully read from db");
inner_deferred.resolve(res);
})
.fail(function(err){
inner_deferred.reject(err);
});
return inner_deferred.promise;
})
.fail(function(err){
deferred.reject(err);
})
.finally(function(){
deferred.resolve();
});
}
deferred.promise.nodeify(callback);
return deferred.promise;
}