-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
135 lines (115 loc) · 3.26 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
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
var bodyParser = require('body-parser');
var compress = require('compression');
var connectMongo = require('connect-mongo');
var errorhandler = require('errorhandler');
var express = require('express');
var expressValidator = require('express-validator');
var favicon = require('serve-favicon');
var flash = require('express-flash');
var lusca = require('lusca');
var methodOverride = require('method-override');
var moment = require('moment');
var mongoose = require('mongoose');
var morgan = require('morgan');
var passport = require('passport');
var path = require('path');
var session = require('express-session');
var stylus = require('stylus');
var csp = require('./lib/csp');
var routes = require('./config/routes');
var secrets = require('./config/secrets');
var validators = require('./lib/validators');
var app = express();
var MongoStore = connectMongo({ session: session });
var week = 604800000;
/**
* Connect to MongoDB.
*/
mongoose.connect(secrets.db);
mongoose.connection.on('error', function() {
console.error('MongoDB Connection Error. Make sure MongoDB is running.');
});
/**
* App configuration
*/
// view engine setup
app.set('views', path.join(__dirname, 'app/views'));
app.set('view engine', 'jade');
app.use(stylus.middleware(path.join(__dirname, 'public')));
if (app.get('env') === 'development') {
// don't minify html
app.locals.pretty = true;
// turn on console logging
app.use(morgan('dev'));
}
if (app.get('env') === 'production') {
app.use(lusca.csp(csp));
app.use(lusca.xframe('DENY'));
app.use(lusca.p3p('ABCDEF'));
app.use(lusca.hsts({ maxAge: 7776000000 })); // 90 days
app.use(lusca.xssProtection(true));
app.set('trust proxy', 1); // trust first proxy
}
app.use(compress());
app.use(favicon(__dirname + '/public/favicon.ico', { maxAge: week }));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(methodOverride());
app.use(expressValidator({ customValidators: validators }));
app.use(session({
resave: false,
saveUninitialized: false,
secret: secrets.session,
cookie: {
httpOnly: true,
maxAge: week
},
store: new MongoStore({
url: secrets.db,
autoReconnect: true
})
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
app.use(lusca.csrf());
/**
* Make local variables avaliable in templates.
*/
app.locals.env = app.get('env');
app.locals.moment = moment;
app.use(function(req, res, next) {
res.locals.baseUrl = req.protocol + '://' + req.get('host') + '/';
res.locals.user = req.user;
next();
});
// static cache for one week
app.use(express.static(path.join(__dirname, 'public'), { maxAge: week }));
// routes setup
app.use('/', routes);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Page Not Found');
err.status = 404;
next(err);
});
/**
* Error handlers
*/
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(errorhandler());
}
// production error handler
// no stacktraces leaked to user
if (app.get('env') === 'production') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
title: 'Page not found',
message: err.message
});
});
}
module.exports = app;