-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
63 lines (51 loc) · 1.54 KB
/
server.js
File metadata and controls
63 lines (51 loc) · 1.54 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
56
57
58
59
60
61
62
63
/*
Based on https://github.com/madhums/node-express-mongoose-demo
*/
// Imports
var debug = require('debug')('instacase');
debug('Booting Instacase');
var express = require('express');
var fs = require('fs');
var passport = require('passport');
// Load server config
var env = process.env.NODE_ENV || 'development';
var config = require('./config/config')[env]
// Set up the database
var mongoose = require('mongoose');
debug('Connecting to MongoDB at ' + config.db);
var connect = function() {
mongoose.connect(config.db, {
server: {
socketOptions: {
keepAlive: 1
}
}
});
};
connect();
mongoose.connection.on('error', function(err) {
console.error('Could not connect to MongoDB at ' + config.db + '. ' + err);
});
// Automatically reconnect to the database
mongoose.connection.on('disconnected', function() {
connect();
});
// Set up models
var models_path = __dirname + '/app/models';
fs.readdirSync(models_path).forEach(function(file) {
if (file.indexOf('.js') === file.length - 3) require(models_path + '/' + file);
});
// OAuth config
require('./config/passport')(passport, config);
// Set up the app!
var app = express();
// Load express settings
require('./config/express')(app, config, passport);
// Start the app!
app.set('port', process.env.PORT || 3000);
debug('Setting port to ' + app.get('port') + ' and starting server');
app.listen(app.get('port'), function() {
debug('Express server listening on port ' + this.address().port);
});
// expose the app
module.exports = app;