Deprecate getters for express 3.x middleware

In LoopBack 3.0, we are removing these getters, see #2394.
This commit is contained in:
Miroslav Bajtoš 2016-05-31 18:48:47 +02:00
parent 25fe4970e6
commit 75da4c7784
3 changed files with 22 additions and 10 deletions

View File

@ -4,6 +4,7 @@
// License text available at https://opensource.org/licenses/MIT // License text available at https://opensource.org/licenses/MIT
var path = require('path'); var path = require('path');
var deprecated = require('depd')('loopback');
var middlewares = exports; var middlewares = exports;
@ -29,12 +30,10 @@ var middlewareModules = {
'cookieParser': 'cookie-parser', 'cookieParser': 'cookie-parser',
'cookieSession': 'cookie-session', 'cookieSession': 'cookie-session',
'csrf': 'csurf', 'csrf': 'csurf',
'errorHandler': 'errorhandler',
'session': 'express-session', 'session': 'express-session',
'methodOverride': 'method-override', 'methodOverride': 'method-override',
'logger': 'morgan', 'logger': 'morgan',
'responseTime': 'response-time', 'responseTime': 'response-time',
'favicon': 'serve-favicon',
'directory': 'serve-index', 'directory': 'serve-index',
// 'static': 'serve-static', // 'static': 'serve-static',
'vhost': 'vhost' 'vhost': 'vhost'
@ -44,14 +43,20 @@ middlewares.bodyParser = safeRequire('body-parser');
middlewares.json = middlewares.bodyParser && middlewares.bodyParser.json; middlewares.json = middlewares.bodyParser && middlewares.bodyParser.json;
middlewares.urlencoded = middlewares.bodyParser && middlewares.bodyParser.urlencoded; middlewares.urlencoded = middlewares.bodyParser && middlewares.bodyParser.urlencoded;
['bodyParser', 'json', 'urlencoded'].forEach(function(name) {
if (!middlewares[name]) return;
middlewares[name] = deprecated.function(
middlewares[name],
deprecationMessage(name, 'body-parser'));
});
for (var m in middlewareModules) { for (var m in middlewareModules) {
var moduleName = middlewareModules[m]; var moduleName = middlewareModules[m];
middlewares[m] = safeRequire(moduleName) || createMiddlewareNotInstalled(m, moduleName); middlewares[m] = safeRequire(moduleName) || createMiddlewareNotInstalled(m, moduleName);
deprecated.property(middlewares, m, deprecationMessage(m, moduleName));
} }
// serve-favicon requires a path function deprecationMessage(accessor, moduleName) {
var favicon = middlewares.favicon; return 'loopback.' + accessor + ' is deprecated. ' +
middlewares.favicon = function(icon, options) { 'Use `require(\'' + moduleName + '\');` instead.';
icon = icon || path.join(__dirname, '../favicon.ico'); }
return favicon(icon, options);
};

View File

@ -3,8 +3,14 @@
// This file is licensed under the MIT License. // This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT // License text available at https://opensource.org/licenses/MIT
var favicon = require('serve-favicon');
var path = require('path');
/** /**
* Serve the LoopBack favicon. * Serve the LoopBack favicon.
* @header loopback.favicon() * @header loopback.favicon()
*/ */
module.exports = require('../../lib/express-middleware').favicon; module.exports = function(icon, options) {
icon = icon || path.join(__dirname, '../../favicon.ico');
return favicon(icon, options);
};

View File

@ -3,6 +3,7 @@
// This file is licensed under the MIT License. // This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT // License text available at https://opensource.org/licenses/MIT
var cookieParser = require('cookie-parser');
var loopback = require('../'); var loopback = require('../');
var extend = require('util')._extend; var extend = require('util')._extend;
var Token = loopback.AccessToken.extend('MyToken'); var Token = loopback.AccessToken.extend('MyToken');
@ -500,7 +501,7 @@ function createTestApp(testToken, settings, done) {
var app = loopback(); var app = loopback();
app.use(loopback.cookieParser('secret')); app.use(cookieParser('secret'));
app.use(loopback.token(tokenSettings)); app.use(loopback.token(tokenSettings));
app.get('/token', function(req, res) { app.get('/token', function(req, res) {
res.cookie('authorization', testToken.id, {signed: true}); res.cookie('authorization', testToken.id, {signed: true});