forked from N4SJAMK/teamboard-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
63 lines (52 loc) · 1.56 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
'use strict';
var config = require('./config');
var app = require('./config/express');
var mongoose = require('./config/mongoose');
var passport = require('./config/passport');
// Use 'passport' authentication.
app.use(passport.initialize());
// Attach 'CORS' middleware to every route.
app.all('*', require('cors')({
exposedHeaders: ['x-access-token']
}));
// Setup API Routes.
app.use('/api', require('./routes/auth'));
app.use('/api', require('./routes/board'));
/**
* Error handling middleware. All errors passed to 'next' will eventually end
* up here.
*/
app.use(function(err, req, res, next) {
var boom = require('boom');
err = boom.wrap(err, err.status);
if(err.output.statusCode >= 500) {
console.error(err);
}
return res.status(err.output.statusCode).send(err.output.payload);
});
/**
* The Express application.
*/
module.exports.app = app;
/**
* Perform necessary initialization to start the server.
*
* @param {function} onListen Callback invoked when the server starts
* listening to incoming requests.
*/
module.exports.listen = function(onListen) {
mongoose.connect(config.mongo.url, config.mongo.options);
this.server = app.listen(config.port, onListen || function() {
console.log('server listening at', config.port);
});
}
/**
* Perform necessary teardown to stop the server.
*
* @param {function=} onShutdown Callback invoked after shutting down.
*/
module.exports.shutdown = function(onShutdown) {
return this.server.close(function() {
mongoose.disconnect(onShutdown || function() {});
});
}