2015-06-29 22:51:29 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies.
|
|
|
|
*/
|
|
|
|
var fs = require('fs-extra'),
|
|
|
|
http = require('http'),
|
|
|
|
https = require('https'),
|
|
|
|
express = require('express'),
|
|
|
|
morgan = require('morgan'),
|
|
|
|
logger = require('./logger'),
|
|
|
|
bodyParser = require('body-parser'),
|
|
|
|
session = require('express-session'),
|
|
|
|
compression = require('compression'),
|
|
|
|
methodOverride = require('method-override'),
|
|
|
|
cookieParser = require('cookie-parser'),
|
|
|
|
helmet = require('helmet'),
|
|
|
|
multer = require('multer'),
|
|
|
|
passport = require('passport'),
|
2015-07-13 22:11:40 +00:00
|
|
|
raven = require('raven'),
|
2015-11-05 22:07:56 +00:00
|
|
|
MongoStore = require('connect-mongo')(session),
|
2015-06-29 22:51:29 +00:00
|
|
|
flash = require('connect-flash'),
|
|
|
|
config = require('./config'),
|
|
|
|
consolidate = require('consolidate'),
|
2015-07-13 23:27:26 +00:00
|
|
|
path = require('path'),
|
|
|
|
client = new raven.Client(config.DSN);
|
2015-06-29 22:51:29 +00:00
|
|
|
|
2015-08-07 21:02:44 +00:00
|
|
|
|
2015-07-13 22:11:40 +00:00
|
|
|
|
2015-06-29 22:51:29 +00:00
|
|
|
module.exports = function(db) {
|
|
|
|
// Initialize express app
|
|
|
|
var app = express();
|
|
|
|
|
|
|
|
// Globbing model files
|
|
|
|
config.getGlobbedFiles('./app/models/**/*.js').forEach(function(modelPath) {
|
|
|
|
require(path.resolve(modelPath));
|
|
|
|
});
|
|
|
|
|
|
|
|
// Setting application local variables
|
|
|
|
app.locals.title = config.app.title;
|
|
|
|
app.locals.description = config.app.description;
|
|
|
|
app.locals.keywords = config.app.keywords;
|
|
|
|
app.locals.facebookAppId = config.facebook.clientID;
|
2015-07-03 19:25:02 +00:00
|
|
|
|
|
|
|
app.locals.bowerJSFiles = config.getBowerJSAssets();
|
|
|
|
app.locals.bowerCssFiles = config.getBowerCSSAssets();
|
|
|
|
app.locals.bowerOtherFiles = config.getBowerOtherAssets();
|
|
|
|
|
2015-06-29 22:51:29 +00:00
|
|
|
app.locals.jsFiles = config.getJavaScriptAssets();
|
|
|
|
app.locals.cssFiles = config.getCSSAssets();
|
|
|
|
|
|
|
|
// Passing the request url to environment locals
|
|
|
|
app.use(function(req, res, next) {
|
2015-07-27 18:11:43 +00:00
|
|
|
if(config.baseUrl === ''){
|
|
|
|
config.baseUrl = req.protocol + '://' + req.headers.host;
|
|
|
|
}
|
|
|
|
res.locals.url = req.protocol + '://' + req.headers.host + req.url;
|
2015-06-29 22:51:29 +00:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Should be placed before express.static
|
|
|
|
app.use(compression({
|
|
|
|
// only compress files for the following content types
|
|
|
|
filter: function(req, res) {
|
|
|
|
return (/json|text|javascript|css/).test(res.getHeader('Content-Type'));
|
|
|
|
},
|
|
|
|
// zlib option for compression level
|
|
|
|
level: 3
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Showing stack errors
|
|
|
|
app.set('showStackError', true);
|
|
|
|
|
2015-07-13 21:47:24 +00:00
|
|
|
|
2015-06-29 22:51:29 +00:00
|
|
|
// Set swig as the template engine
|
|
|
|
app.engine('server.view.html', consolidate[config.templateEngine]);
|
|
|
|
|
|
|
|
// Set views path and view engine
|
|
|
|
app.set('view engine', 'server.view.html');
|
|
|
|
app.set('views', './app/views');
|
|
|
|
|
|
|
|
// Enable logger (morgan)
|
|
|
|
app.use(morgan(logger.getLogFormat(), logger.getLogOptions()));
|
|
|
|
|
|
|
|
// Environment dependent middleware
|
|
|
|
if (process.env.NODE_ENV === 'development') {
|
|
|
|
// Disable views cache
|
|
|
|
app.set('view cache', false);
|
|
|
|
} else if (process.env.NODE_ENV === 'production') {
|
|
|
|
app.locals.cache = 'memory';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request body parsing middleware should be above methodOverride
|
|
|
|
app.use(bodyParser.urlencoded({
|
|
|
|
extended: true
|
|
|
|
}));
|
|
|
|
app.use(bodyParser.json());
|
|
|
|
app.use(methodOverride());
|
|
|
|
|
|
|
|
// Use helmet to secure Express headers
|
|
|
|
app.use(helmet.xframe());
|
|
|
|
app.use(helmet.xssFilter());
|
|
|
|
app.use(helmet.nosniff());
|
|
|
|
app.use(helmet.ienoopen());
|
|
|
|
app.disable('x-powered-by');
|
|
|
|
|
|
|
|
// Setting the app router and static folder
|
2015-07-27 18:11:43 +00:00
|
|
|
app.use('/', express.static(path.resolve('./public')));
|
|
|
|
app.use('/uploads', express.static(path.resolve('./uploads')));
|
2015-06-29 22:51:29 +00:00
|
|
|
|
2015-11-03 19:55:11 +00:00
|
|
|
// Setting the pdf upload route and folder
|
|
|
|
app.use(multer({ dest: config.tmpUploadPath,
|
|
|
|
rename: function (fieldname, filename) {
|
|
|
|
return Date.now();
|
|
|
|
},
|
|
|
|
onFileUploadStart: function (file) {
|
|
|
|
//Check to make sure we can only upload images and pdfs
|
|
|
|
console.log(file.originalname + ' is starting ...');
|
|
|
|
},
|
|
|
|
onFileUploadComplete: function (file, req, res) {
|
|
|
|
console.log(file.originalname + ' uploaded to ' + file.path);
|
|
|
|
// console.log('\n\nheadersSent in onFileUploadComplete: ', res.headersSent);
|
|
|
|
// res.status(200).send(file);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
2015-06-29 22:51:29 +00:00
|
|
|
// CookieParser should be above session
|
|
|
|
app.use(cookieParser());
|
|
|
|
|
|
|
|
// Express MongoDB session storage
|
|
|
|
app.use(session({
|
|
|
|
saveUninitialized: true,
|
|
|
|
resave: true,
|
|
|
|
secret: config.sessionSecret,
|
2015-11-05 22:07:56 +00:00
|
|
|
store: new MongoStore({
|
|
|
|
mongooseConnection: db.connection,
|
|
|
|
collection: config.sessionCollection
|
|
|
|
}),
|
2015-06-29 22:51:29 +00:00
|
|
|
cookie: config.sessionCookie,
|
2015-11-03 19:55:11 +00:00
|
|
|
name: config.sessionName
|
2015-06-29 22:51:29 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
// use passport session
|
|
|
|
app.use(passport.initialize());
|
|
|
|
app.use(passport.session());
|
|
|
|
|
|
|
|
// connect flash for flash messages
|
|
|
|
app.use(flash());
|
|
|
|
|
|
|
|
// Globbing routing files
|
|
|
|
config.getGlobbedFiles('./app/routes/**/*.js').forEach(function(routePath) {
|
|
|
|
require(path.resolve(routePath))(app);
|
|
|
|
});
|
|
|
|
|
2015-07-21 23:25:45 +00:00
|
|
|
// Add headers for Sentry
|
2015-08-07 21:02:44 +00:00
|
|
|
app.use(function (req, res, next) {
|
2015-07-21 23:25:45 +00:00
|
|
|
|
|
|
|
// Website you wish to allow to connect
|
|
|
|
res.setHeader('Access-Control-Allow-Origin', 'http://sentry.polydaic.com');
|
|
|
|
|
|
|
|
// Request methods you wish to allow
|
|
|
|
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
|
|
|
|
|
|
|
|
// Request headers you wish to allow
|
|
|
|
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
|
|
|
|
|
|
|
|
// Set to true if you need the website to include cookies in the requests sent
|
|
|
|
// to the API (e.g. in case you use sessions)
|
|
|
|
res.setHeader('Access-Control-Allow-Credentials', true);
|
|
|
|
|
|
|
|
// Pass to next layer of middleware
|
|
|
|
next();
|
|
|
|
});
|
2015-07-13 22:33:36 +00:00
|
|
|
|
2015-07-13 23:27:26 +00:00
|
|
|
// Sentry (Raven) middleware
|
|
|
|
app.use(raven.middleware.express.requestHandler(config.DSN));
|
2015-07-13 22:33:36 +00:00
|
|
|
|
2015-07-13 23:27:26 +00:00
|
|
|
// Should come before any other error middleware
|
|
|
|
app.use(raven.middleware.express.errorHandler(config.DSN));
|
2015-07-13 22:33:36 +00:00
|
|
|
|
2015-06-29 22:51:29 +00:00
|
|
|
// Assume 'not found' in the error msgs is a 404. this is somewhat silly, but valid, you can do whatever you like, set properties, use instanceof etc.
|
|
|
|
app.use(function(err, req, res, next) {
|
|
|
|
// If the error object doesn't exists
|
|
|
|
if (!err) return next();
|
|
|
|
|
|
|
|
// Log it
|
|
|
|
console.error(err.stack);
|
2015-07-13 23:27:26 +00:00
|
|
|
client.captureError(err);
|
2015-06-29 22:51:29 +00:00
|
|
|
|
|
|
|
// Error page
|
|
|
|
res.status(500).render('500', {
|
|
|
|
error: err.stack
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Assume 404 since no middleware responded
|
|
|
|
app.use(function(req, res) {
|
2015-07-13 23:27:26 +00:00
|
|
|
client.captureError(new Error('Page Not Found'));
|
2015-06-29 22:51:29 +00:00
|
|
|
res.status(404).render('404', {
|
|
|
|
url: req.originalUrl,
|
|
|
|
error: 'Not Found'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
if (process.env.NODE_ENV === 'secure') {
|
|
|
|
// Load SSL key and certificate
|
|
|
|
var privateKey = fs.readFileSync('./config/sslcerts/key.pem', 'utf8');
|
|
|
|
var certificate = fs.readFileSync('./config/sslcerts/cert.pem', 'utf8');
|
|
|
|
|
|
|
|
// Create HTTPS Server
|
|
|
|
var httpsServer = https.createServer({
|
|
|
|
key: privateKey,
|
|
|
|
cert: certificate
|
|
|
|
}, app);
|
|
|
|
|
|
|
|
// Return HTTPS server instance
|
|
|
|
return httpsServer;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return Express server instance
|
|
|
|
return app;
|
|
|
|
};
|