2016-05-04 00:10:46 +00:00
|
|
|
// Copyright IBM Corp. 2014,2016. All Rights Reserved.
|
|
|
|
// Node module: loopback
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2014-05-29 17:18:58 +00:00
|
|
|
var path = require('path');
|
2016-05-31 16:48:47 +00:00
|
|
|
var deprecated = require('depd')('loopback');
|
2014-05-29 17:18:58 +00:00
|
|
|
|
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',
|
|
|
|
'session': 'express-session',
|
|
|
|
'methodOverride': 'method-override',
|
|
|
|
'logger': 'morgan',
|
|
|
|
'responseTime': 'response-time',
|
|
|
|
'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;
|
|
|
|
|
2016-05-31 16:48:47 +00:00
|
|
|
['bodyParser', 'json', 'urlencoded'].forEach(function(name) {
|
|
|
|
if (!middlewares[name]) return;
|
|
|
|
middlewares[name] = deprecated.function(
|
|
|
|
middlewares[name],
|
|
|
|
deprecationMessage(name, 'body-parser'));
|
|
|
|
});
|
|
|
|
|
2014-05-29 17:18:58 +00:00
|
|
|
for (var m in middlewareModules) {
|
|
|
|
var moduleName = middlewareModules[m];
|
|
|
|
middlewares[m] = safeRequire(moduleName) || createMiddlewareNotInstalled(m, moduleName);
|
2016-05-31 16:48:47 +00:00
|
|
|
deprecated.property(middlewares, m, deprecationMessage(m, moduleName));
|
2014-05-29 17:18:58 +00:00
|
|
|
}
|
|
|
|
|
2016-05-31 16:48:47 +00:00
|
|
|
function deprecationMessage(accessor, moduleName) {
|
|
|
|
return 'loopback.' + accessor + ' is deprecated. ' +
|
|
|
|
'Use `require(\'' + moduleName + '\');` instead.';
|
|
|
|
}
|