-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
54 lines (46 loc) · 1.43 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
'use strict';
const restify = require('restify');
const mongoose = require('mongoose');
module.exports = (config, callback=undefined) => {
const server = restify.createServer({
name: config.name,
version: config.version,
});
server.use(restify.plugins.jsonBodyParser({mapParams: true}));
server.use(restify.plugins.acceptParser(server.acceptable));
server.use(restify.plugins.queryParser({mapParams: true}));
server.use(restify.plugins.fullResponse());
server.listen(config.port, () => {
let options = {
promiseLibrary: global.Promise,
autoReconnect: true,
reconnectTries: Number.MAX_VALUE,
reconnectInterval: 1000,
// useMongoClient: true,
};
mongoose.Promise = options.promiseLibrary;
let connection = mongoose.connection.openUri(config.db.uri, options);
connection
.then((db) => {
require('./routes/index')(server);
console.log(
'%s v%s ready to accept connections '+
'on %s in %s environment.',
server.name,
config.version,
server.url,
config.env
);
typeof callback === 'function' && callback();
})
.catch((err) => {
console.error('An error occurred while '+
'attempting to connect to MongoDB:\n', err.message);
process.exit(1);
});
server.on('close', (callback) => {
connection.close(callback);
});
});
return server;
};