-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
69 lines (52 loc) · 1.53 KB
/
app.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
64
65
66
67
68
69
/**
* Starting point of the express app
*/
var express = require('express')
, sio = require('socket.io')()
, mongoose = require('mongoose')
, fs = require('fs')
, config = require('config');
/**
* Initialize Database
*/
// Open connection to mongodb
var connect = function () {
var options = { server: { socketOptions: { keepAlive: 1 } } };
mongoose.connect(config.db, options);
};
connect();
mongoose.connection.on('error', console.log);
// This can be good for production
// mongoose.connection.on('disconnected', connect);
// Bootstrap models
fs.readdirSync('./app/models').forEach(function (file) {
if (~file.indexOf('.js')) require('./app/models/' + file);
// Database needs to be clean when testing
if(process.env.NODE_ENV === 'test') {
mongoose.model(file.charAt(0).toUpperCase() + file.substring(1, file.length - 3)).remove({}, function(err) {
if(err) console.log(err);
});
}
});
/**
* Initialize App
*/
var app = express();
// Mount the middleware to the express
// app, e.g. the body parser, logger,
// view engines, ...
require('./app/middleware')(app, sio);
// All requests will be passed to the
// router and `function(req, res)` will
// do its thing.
require('./app/router')(app);
// If something is passed through the
// router, an error occured and will
// be handled here.
require('./app/error-handler')(app);
// After the errorHandler everything
// is processed. */
module.exports.express = app;
module.exports.sio = sio;
// The app will be passed to bin/www
// where the server will be started.