-
Notifications
You must be signed in to change notification settings - Fork 11
/
app.js
297 lines (262 loc) · 8.17 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
var express = require('express');
var expressValidator = require('express-validator');
var path = require('path');
var favicon = require('serve-favicon');
var fs = require('fs');
var logger;
var morgan = require('morgan');
var winston = require('winston');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var stylus = require('stylus');
var i18n = require('i18n');
var helmet = require('helmet');
var csrf = require('csurf');
var passport = require('passport');
var flash = require('connect-flash');
var session = require('express-session');
var methodOverride = require('method-override');
var utils = require('./utils');
var config = exports.config = require('./config');
var mail = exports.mail = require('./utils/mailer.js');
var anyandgo = exports.anyandgo = {};
var User = require('./models/user.js');
// Anyandgo
anyandgo.models = [];
// Error
process.on('uncaughtException', function(err) {
if(logger) {
logger.error(err.stack);
} else {
console.log("Exception", err.stack);
}
});
// Express
var app = exports.app = express();
app.set("envflag", config.envflag || process.env.NODE_ENV);
app.set("autologin", config.autologin || {});
// Setup vars
app.use(function(req, res, next){
res.locals.envflag = config.envflag || process.env.NODE_ENV;
res.locals.path = req.path;
res.locals.autologin = config.autologin || {};
// Analytics
if (config.analytics && config.analytics.enabled) {
res.locals.tracking = config.analytics.tracking;
}
next();
});
// Database Connection
var dbConex = exports.dbConex = utils.dbConnection(config.db.domain,config.db.name,config.db.user,config.db.pass);
// DB Fixtures
if (config.fixtures && config.fixtures === "enabled") {
// Load Fixtures
require('./fixtures');
}
// Security
if (config.security && config.security === "enabled") {
app.use(helmet());
app.use(helmet.hidePoweredBy({ setTo: 'PHP 4.2.0' }));
app.use(helmet.noSniff());
app.use(helmet.frameguard('deny'));
}
// i18n setup
i18n.configure({
// setup some locales: other locales default to en silently
locales:[
//global:translation:start
//global:translation:end
'en-us',
'es-ar'],
// sets a custom cookie name to parse locale settings from - defaults to NULL
cookie: 'lang',
// where to store json files - defaults to './locales' relative to modules directory
directory: __dirname + '/locales',
defaultLocale: 'es-ar'
});
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// stylus setup
app.use('/css', stylus.middleware({
src: __dirname + '/views/stylus',
dest: __dirname + '/public/css',
compile: function (str, path) {
return stylus(str)
.set('filename', path)
.set('compress', true);
}
}));
// app.use(favicon(__dirname + '/public/img/favicon.ico'));
// Logs
// [Predefined Formats](https://github.com/expressjs/morgan#predefined-formats)
var loggerConfigs = {
levels: { HTTP: 0, debug: 1, info: 2, warn: 3, error: 3 },
colors: { HTTP: 'blue', debug: 'white', info: 'green', warn: 'yellow', error: 'red' }
};
winston.addColors(loggerConfigs.colors);
if (typeof config.app.logs !== 'undefined' && config.app.logs.enabled) {
// create a write stream (in append mode)
//var accessLogStream = fs.createWriteStream(__dirname + '/' + config.app.logs.file, {flags: 'a'});
logger = new winston.Logger({
levels: loggerConfigs.levels,
transports: [
new winston.transports.File({
name: 'file-log',
level: 'HTTP',
filename: __dirname + '/' + config.app.logs.file,
handleExceptions: false,
json: true,
maxsize: 5242880, //5MB
maxFiles: 5,
colorize: false
}),
new winston.transports.Console({
name: 'console-log',
level: 'HTTP',
handleExceptions: false,
json: false,
colorize: true
})
],
exitOnError: false
});
logger.stream = {
write: function(message, encoding){
logger.log("HTTP",message.slice(0, -1));
}
};
// setup the logger
app.use(morgan(config.app.logs.format || 'dev', {stream: logger.stream }));
// remember to see the log:
// $ touch access.log
// $ tail -f access.log
} else {
//app.use(logger('dev'));
logger = new winston.Logger({
levels: loggerConfigs.levels,
transports: [
new winston.transports.Console({
name: 'console-log',
level: 'HTTP',
handleExceptions: false,
json: false,
colorize: true
})
],
exitOnError: false
});
logger.stream = {
write: function(message, encoding){
logger.log("HTTP",message.slice(0, -1));
}
};
// setup the logger
app.use(morgan('dev', {stream: logger.stream }));
}
exports.logger = logger;
// Log example
logger.info("Starting anyandgo...");
logger.info("For more information visit http://anyandgo.io/ ");
app.use(bodyParser.json());
app.use(expressValidator());
app.use(bodyParser.urlencoded({
extended: true
}));
// method override + restify settings
// https://www.npmjs.org/package/express-restify-mongoose
app.use(methodOverride());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// i18n init parses req for language headers, cookies, etc.
app.use(i18n.init);
// Passport
app.use(session({ secret: 'secret', saveUninitialized: true, resave: true })); // session secret
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session
// CSRF Security
// http://stackoverflow.com/questions/23997572/error-misconfigured-csrf-express-js-4
if (config.csrf && config.csrf === "enabled") {
//app.use(csrf({ cookie: { key: 'XSRF-TOKEN' } }));
app.use(csrf());
// Angular
// https://github.com/expressjs/csurf/issues/13
// AngularJS looks for a cookie named "XSRF-TOKEN" and sets the header as "X-XSRF-TOKEN"
/*
app.use(function (req, res, next) {
res.cookie('XSRF-TOKEN', req.csrfToken());
next();
});
*/
app.use(function (req, res) {
var token = req.csrfToken();
// angular
res.cookie('XSRF-TOKEN', req.csrfToken());
// field
res.locals._csrf = req.csrfToken();
res.locals.csrf_form_html = '<input type="hidden" name="_csrf" value="' + req.csrfToken() + '" >';
req.next();
});
// error handler
app.use(function (err, req, res, next) {
if (err.code !== 'EBADCSRFTOKEN') return next(err);
// handle CSRF token errors here
res.status(403);
res.send('sorry your request was invalid');
});
}
// used to serialize the user for the session
passport.serializeUser(function(user, done) {
//console.log(user);
if(user.role === 'user') {
User.findOne({ _id : user._id }, function(err, usr){
done(err, usr);
});
} else {
done(null, user);
}
});
// used to deserialize the user
passport.deserializeUser(function(user, done) {
done(null, user);
});
// Interceptors
app.use(function(req, res, next) {
res.locals.user = req.user;
res.locals.flash = req.flash();
next();
});
// Routes
require('./routes/auth');
require('./routes/main');
/// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
/// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err,
title: 'error'
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {},
title: 'error'
});
});
module.exports = app;