-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
123 lines (99 loc) · 3.76 KB
/
index.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
var config = require("./config/"),
crave = require('crave'),
express = require('express'),
compress = require('compression'),
passport = require('passport'),
session = require('express-session'),
bodyParser = require('body-parser'),
i18n = require('i18next'),
path = require('path');
var log = new (require('./libs/log'))(config.log),
database = new (require('./libs/database/'))(config, log),
oauth2 = new (require('./libs/oauth2/'))(config, log),
responseHandler = new (require('seedio-response'))(config, log),
MongoStore = require('connect-mongo')(session);
// Initialize internationalization options.
i18n.init(config.i18n);
// Create an express application object.
var app = module.exports = express();
// If the cookie is secure and proxy is enabled. We need to enable express' trust proxy for it set cookies correctly.
if(config.session.cookie.secure && config.session.proxy) {
app.enable('trust proxy');
}
// Disable the "X-Powered-By: Express" HTTP header, which is enabled by default.
app.disable("x-powered-by");
// Log all incoming requests, if enabled in config.
if(config.server.requests) {
app.use(log.requestLogger());
}
// Enable G-ZIP compression.
app.use(compress());
// Parse url encoded json, "Content-Type: application/x-www-form-urlencoded"
app.use(bodyParser.urlencoded({ extended: false}));
// Parse bodies with json, "Content-Type: application/json"
app.use(bodyParser.json());
// Adds i18n object to req object
app.use(i18n.handle);
// TODO: Set-up Views engines
app.set('view engine', null);
//app.set('views','./client/views');
//app.set('view engine', 'jade');
// Make public folder static so it can be served
app.use(express.static('./client/public', config.express.static));
// Extend express response object.
app.use(responseHandler.addSetMethods);
// Allows use of the translate function inside of a Jade template.
i18n.registerAppHelper(app);
// Configure express sessions
app.use(session({
name: config.session.name,
secret: config.session.secret,
cookie: {
maxAge: config.session.maxAge,
secure: config.session.cookie.secure
},
resave: config.session.resave,
saveUninitialized: config.session.saveUninitialized,
store: new MongoStore({
url: config.database.uri,
ttl: config.session.cookie.ttl
})
}));
app.use(passport.initialize());
app.use(passport.session());
// Method to connect to database and start the server.
var start = function(err) {
if(err) {
return log.error(err);
}
database.connect(function(err) {
if(err) {
log.info('Failed to connect');
return log.error(err);
}
oauth2.createOauth2Server(app, config, log, function(err) {
if(err) {
return log.error(err);
}
});
// Final middleware to format standard responses.
app.use(responseHandler.responseHandler(function(req, res, next) {
// TODO: Add any logic needed to handle a view not being found.
next();
}));
// Final middleware to format any error responses.
app.use(responseHandler.errorHandler(function(err, req, res, next) {
// TODO: Add any logic needed to render error views.
next();
}));
var server = app.listen(config.server.port, function() {
var serverInfo = this.address();
var address = (serverInfo.address === "0.0.0.0" || serverInfo.address === "::") ? "localhost" : serverInfo.address;
log.info("Listening on http://%s:%s with database %s", address, serverInfo.port, config.database.uri.replace(/mongodb:\/\/(.*:.*)@/ig, ''));
});
});
};
// Configure Crave.
crave.setConfig(config.crave);
// Recursively load all files of the specified type(s) that are also located in the specified folder.
crave.directory(path.resolve("./app"), [ "model", "api", "controller2" ], start, app, config, log, oauth2);