insight-ui-zcash/config/express.js

84 lines
2.0 KiB
JavaScript
Raw Normal View History

2014-01-06 08:33:58 -08:00
'use strict';
/**
* Module dependencies.
*/
var express = require('express'),
helpers = require('view-helpers'),
config = require('./config');
2014-01-06 08:33:58 -08:00
2014-01-19 05:09:59 -08:00
module.exports = function(app, historicSync) {
2014-01-06 09:37:32 -08:00
app.set('showStackError', true);
2014-01-06 08:33:58 -08:00
2014-01-06 09:37:32 -08:00
//Prettify HTML
app.locals.pretty = true;
2014-01-06 08:33:58 -08:00
2014-01-06 09:37:32 -08:00
//Should be placed before express.static
app.use(express.compress({
filter: function(req, res) {
return (/json|text|javascript|css/).test(res.getHeader('Content-Type'));
},
level: 9
}));
2014-01-06 08:33:58 -08:00
2014-01-06 09:37:32 -08:00
//Set views path, template engine and default layout
app.set('views', config.root + '/app/views');
app.set('view engine', 'jade');
2014-01-06 08:33:58 -08:00
2014-01-06 09:37:32 -08:00
//Enable jsonp
2014-01-19 05:09:59 -08:00
app.enable('jsonp callback');
//custom middleware
function setHistoric(req, res, next) {
2014-01-21 14:13:21 -08:00
req.historicSync = historicSync;
2014-01-19 05:09:59 -08:00
next();
}
app.use('/api/sync', setHistoric);
2014-01-06 09:37:32 -08:00
app.configure(function() {
2014-01-19 05:09:59 -08:00
2014-01-06 09:37:32 -08:00
//cookieParser should be above session
app.use(express.cookieParser());
2014-01-06 08:33:58 -08:00
2014-01-06 09:37:32 -08:00
// request body parsing middleware should be above methodOverride
app.use(express.urlencoded());
app.use(express.json());
app.use(express.methodOverride());
2014-01-06 08:33:58 -08:00
//dynamic helpers
app.use(helpers(config.app.name));
2014-01-06 09:37:32 -08:00
//routes should be at the last
app.use(app.router);
2014-01-06 08:33:58 -08:00
2014-01-19 05:09:59 -08:00
2014-01-06 09:37:32 -08:00
//Setting the fav icon and static folder
app.use(express.favicon());
app.use(express.static(config.root + '/public'));
2014-01-06 08:33:58 -08:00
2014-01-06 09:37:32 -08: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) {
//Treat as 404
if (~err.message.indexOf('not found')) return next();
2014-01-06 08:33:58 -08:00
2014-01-06 09:37:32 -08:00
//Log it
console.error(err.stack);
2014-01-06 08:33:58 -08:00
2014-01-06 09:37:32 -08:00
//Error page
res.status(500).render('500', {
error: err.stack
});
});
2014-01-06 08:33:58 -08:00
2014-01-06 09:37:32 -08:00
//Assume 404 since no middleware responded
app.use(function(req, res, next) {
res.status(404).render('404', {
url: req.originalUrl,
error: 'Not found'
});
2014-01-06 08:33:58 -08:00
});
2014-01-06 09:37:32 -08:00
});
};