-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
95 lines (83 loc) · 2.96 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
'use strict';
var express = require('express'),
fs = require('fs'),
passport = require('passport'),
mongoose = require('mongoose');
// Initialize system variables
var config = require('./config/config');
// Set the node enviornment variable if not set before
process.env.NODE_ENV = process.env.NODE_ENV || config.env || 'development';
// Bootstrap models (walk and require all the models (all the *.js and *.coffee files) in the /app/models directory)
var modelsPath = __dirname + '/app/models';
var walk = function(path) {
fs.readdirSync(path).forEach(function(file) {
var newPath = path + '/' + file;
var stat = fs.statSync(newPath);
if (stat.isFile()) {
if (/(.*)\.(js$|coffee$)/.test(file)) {
require(newPath);
}
} else if (stat.isDirectory()) {
walk(newPath);
}
});
};
walk(modelsPath);
// Connect to the database
var db;
mongoose.connection.on('connected', function() {
if (process.env.NODE_ENV === "production") {
console.log('DB connected;\nHost: ' + process.env.OPENSHIFT_MONGODB_DB_HOST + '\nPort: ' + process.env.OPENSHIFT_MONGODB_DB_PORT);
}
});
mongoose.connection.on('disconnected', function() {
console.log('MongoDB disconnected!');
db = mongoose.connect(config.database);
});
mongoose.connection.on('error', function(error) {
console.error('Error in MongoDb connection: ' + error);
mongoose.disconnect();
});
db = mongoose.connect(config.database);
// passport config
require('./config/passport')(passport);
// Express app init and configuration
var app = express();
require('./config/express')(app, passport, db);
try {
// Bootstrap API routes (walk and require all the APIs (all the *.js and *.coffee files) in the /app/api directory)
var apisPath = __dirname + '/app/routes';
var walk = function(path) {
fs.readdirSync(path).forEach(function(file) {
var newPath = path + '/' + file;
var stat = fs.statSync(newPath);
if (stat.isFile()) {
if (/(.*)\.(js$|coffee$)/.test(file)) {
require(newPath)(app, passport);
}
//skip the middlewares directory
} else if (stat.isDirectory() && file !== 'middlewares') {
walk(newPath);
}
});
};
walk(apisPath);
require('./app/router')(app);
} catch (e) {
console.log("Router error: ");
console.log(e);
console.log(e.stack);
}
var port = (process.env.OPENSHIFT_NODEJS_PORT || process.env.OPENSHIFT_INTERNAL_PORT || config.port);
var ipaddr = (process.env.OPENSHIFT_NODEJS_IP || process.env.OPENSHIFT_INTERNAL_IP || config.ip || 'localhost');
exports = module.exports = app;
try {
app.listen(port, ipaddr);
console.log("App listening on port " + port + ". Environment: " + process.env.NODE_ENV + ".");
} catch (e) {
console.log("App.listen error: ");
console.log(e);
console.log(e.stack);
}
/*
*/