-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
55 lines (47 loc) · 1.63 KB
/
server.js
File metadata and controls
55 lines (47 loc) · 1.63 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
import http from 'http';
import mongoose from 'mongoose';
import util from 'util';
import fs from 'fs';
import glob from 'glob';
import Promise from 'bluebird';
import { join } from 'path';
import config from './config/env';
import app from './config/express';
const debug = require('debug')('express-mongoose-es6-rest-api:index');
//require models
const models = join(__dirname, 'server/models');
fs.readdirSync(models)
.filter(file => ~file.search(/^[^\.].*\.js$/))
.forEach(file => require(join(models, file)));
// connect to mongo db
connect()
.on('error', console.log)
.on('disconnected', connect)
.once('open', listen);
function listen() {
const server = http.createServer(app);
const PORT = config.port;
server.listen(PORT, function() {
console.log(`server started on port ${config.port} (${config.env})`);
// 注册全局未捕获异常处理器
process.on('uncaughtException', function(err) {
console.error("Caught exception:", err.stack);
});
process.on('unhandledRejection', function(reason, p) {
console.error("Unhandled Rejection at: Promise ", p, " reason: ", reason.stack);
});
});
}
function connect() {
// print mongoose logs in dev env
var options = { server: { socketOptions: { keepAlive: 1 } } };
return mongoose.connect(config.db, options).connection;
}
if (config.MONGOOSE_DEBUG) {
mongoose.set('debug', (collectionName, method, query, doc) => {
debug(`${collectionName}.${method}`, util.inspect(query, false, 20), doc);
});
}
// make bluebird default Promise
mongoose.Promise = Promise;
export default app;