2014-05-29 17:18:58 +00:00
|
|
|
var express = require('express');
|
|
|
|
var path = require('path');
|
|
|
|
|
2014-06-03 08:15:15 +00:00
|
|
|
var middlewares = exports;
|
2014-05-29 17:18:58 +00:00
|
|
|
|
|
|
|
function safeRequire(m) {
|
|
|
|
try {
|
|
|
|
return require(m);
|
|
|
|
} catch (err) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function createMiddlewareNotInstalled(memberName, moduleName) {
|
2014-10-30 20:49:47 +00:00
|
|
|
return function() {
|
2014-07-22 13:40:36 +00:00
|
|
|
var msg = 'The middleware loopback.' + memberName + ' is not installed.\n' +
|
|
|
|
'Run `npm install --save ' + moduleName + '` to fix the problem.';
|
|
|
|
throw new Error(msg);
|
2014-05-29 17:18:58 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
var middlewareModules = {
|
2014-10-16 23:17:38 +00:00
|
|
|
'compress': 'compression',
|
|
|
|
'timeout': 'connect-timeout',
|
|
|
|
'cookieParser': 'cookie-parser',
|
|
|
|
'cookieSession': 'cookie-session',
|
|
|
|
'csrf': 'csurf',
|
|
|
|
'errorHandler': 'errorhandler',
|
|
|
|
'session': 'express-session',
|
|
|
|
'methodOverride': 'method-override',
|
|
|
|
'logger': 'morgan',
|
|
|
|
'responseTime': 'response-time',
|
|
|
|
'favicon': 'serve-favicon',
|
|
|
|
'directory': 'serve-index',
|
|
|
|
// 'static': 'serve-static',
|
|
|
|
'vhost': 'vhost'
|
2014-05-29 17:18:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
middlewares.bodyParser = safeRequire('body-parser');
|
|
|
|
middlewares.json = middlewares.bodyParser && middlewares.bodyParser.json;
|
|
|
|
middlewares.urlencoded = middlewares.bodyParser && middlewares.bodyParser.urlencoded;
|
|
|
|
|
|
|
|
for (var m in middlewareModules) {
|
|
|
|
var moduleName = middlewareModules[m];
|
|
|
|
middlewares[m] = safeRequire(moduleName) || createMiddlewareNotInstalled(m, moduleName);
|
|
|
|
}
|
|
|
|
|
|
|
|
// serve-favicon requires a path
|
|
|
|
var favicon = middlewares.favicon;
|
2014-10-30 20:49:47 +00:00
|
|
|
middlewares.favicon = function(icon, options) {
|
2014-05-29 17:18:58 +00:00
|
|
|
icon = icon || path.join(__dirname, '../favicon.ico');
|
|
|
|
return favicon(icon, options);
|
|
|
|
};
|