-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·52 lines (41 loc) · 1.08 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
'use strict';
/*
* nodejs-express-mongoose-demo
* Copyright(c) 2013 Madhusudhan Srinivasa <[email protected]>
* MIT Licensed
*/
/**
* Module dependencies
*/
const fs = require('fs');
const join = require('path').join;
const express = require('express');
const mongoose = require('mongoose');
const config = require('./config');
const models = join(__dirname, 'app/models');
const port = process.env.PORT || 3000;
const app = express();
/**
* Expose
*/
module.exports = app;
// Bootstrap models
fs.readdirSync(models)
.filter(file => ~file.search(/^[^\.].*\.js$/))
.forEach(file => require(join(models, file)));
// Bootstrap routes
require('./config/express')(app);
require('./config/routes')(app);
connect()
.on('error', console.log)
.on('disconnected', connect)
.once('open', listen);
function listen () {
if (app.get('env') === 'test') return;
app.listen(port);
console.log('Express app started on port ' + port);
}
function connect () {
var options = { server: { socketOptions: { keepAlive: 1 } } };
return mongoose.connect(config.db, options).connection;
}