-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
41 lines (35 loc) · 971 Bytes
/
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
const mongoose = require('mongoose');
const path = require('path');
const route = require('koa-router');
const fs = require('fs');
const Koa = require('koa');
const app = new Koa();
const config = require('./config');
mongoose.Promise = global.Promise;
const connect = () => {
mongoose.connect(config.db, {
server: { socketOptions: { keepAlive: 120 } }
});
}, conn = mongoose.connection;
connect();
conn.on('error', console.error);
conn.on('disconnected', connect);
conn.on('open', () => {
console.log(`Connected mongo: ${config.db}`);
});
fs.readdirSync(path.join(__dirname, 'app/models')).forEach((file) => {
if (~file.indexOf('.js')) require(path.join(__dirname, 'app/models', file));
});
app.use(async (ctx, next) => {
try {
await next();
} catch (err) {
ctx.throw(500, err)
}
});
config.basic(app);
config.router(app);
const port = process.env.VCAP_APP_PORT || 3300;
app.listen(port, () => {
console.log(`Listening on ${port}`);
});