-
Notifications
You must be signed in to change notification settings - Fork 18
/
server.js
66 lines (56 loc) · 1.58 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
'use strict';
// Module dependencies.
var express = require('express'),
fs = require('fs'),
path = require('path'),
mongoose = require('mongoose'),
passport = require('passport'),
logger = require('mean-logger');
var app = express();
// Load configurations
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
var config = require('./config/config');
// DB Connection
mongoose.connect(config.db);
// Models
var modelsPath = path.join(__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$)/.test(file)) {
require(newPath);
}
} else if(stat.isDirectory()) {
walk(newPath);
}
});
};
walk(modelsPath);
// Express settings
require('./config/passport')(passport);
require('./config/express')(app, passport);
// Routes
var routesPath = path.join(__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$)/.test(file)) {
require(newPath)(app, passport);
}
// Middlewares is not a route itself
} else if(stat.isDirectory() && file !== 'middlewares') {
walk(newPath);
}
});
};
walk(routesPath);
// Start the server and app.
app.listen(app.get('port'), function() {
console.log('Express App listening at port ' + app.get('port'));
});
logger.init(app, passport, mongoose);
exports = module.exports = app;