Remove legacy express 3.x middleware getters

Remove middleware-getter properties that were simlifying upgrade from
LoopBack 1.x/Express 3.x applications:

  - loopback.compress
  - loopback.timeout
  - loopback.cookieParser
  - loopback.cookieSession
  - loopback.csrf
  - loopback.errorHandler
  - loopback.session
  - loopback.methodOverride
  - loopback.logger
  - loopback.responseTime
  - loopback.favicon
  - loopback.directory
  - loopback.vhost

Also remove `loopback.mime`, which was set to `undefined` anyways.
This commit is contained in:
Miroslav Bajtoš 2016-05-31 16:50:04 +02:00
parent 70cec0755a
commit 8d295b70f6
7 changed files with 39 additions and 95 deletions

View File

@ -99,3 +99,31 @@ Developers that are relying on these properties, can redefine them in `user.json
``` ```
Please see [Related code change](https://github.com/strongloop/loopback/pull/2299) here. Please see [Related code change](https://github.com/strongloop/loopback/pull/2299) here.
## Remove getters for Express 3.x middleware
Express 4.x stopped bundling commonly-used middleware. To simplify migration
of LoopBack 1.x applications (powered by Express 3.x) to LoopBack 2.x (powered
by Express 4.x), we created getter properties to allow developers to keep using
the old convention.
We have removed these getters in LoopBack 3.0, here is the full list of
removed properties together with the middleware module name to use instead:
- `loopback.compress` - use `require('compression')` instead
- `loopback.timeout` - use `require('connect-timeout')` instead
- `loopback.cookieParser` - use `require('cookie-parser')` instead
- `loopback.cookieSession` - use `require('cookie-session')` instead
- `loopback.csrf` - use `require('csurf')` instead
- `loopback.errorHandler` - use `require('errorhandler')` instead
- `loopback.session` - use `require('express-session')` instead
- `loopback.methodOverride` - use `require('method-override')` instead
- `loopback.logger` - use `require('morgan')` instead
- `loopback.responseTime` - use `require('response-time')` instead
- `loopback.favicon` - use `require('serve-favicon')` instead
- `loopback.directory` - use `require('serve-index')` instead
- `loopback.vhost` - use `require('vhost')` instead
We have also removed `loopback.mime`, which was always set to `undefined`.
See [loopback#2349](https://github.com/strongloop/loopback/pull/2394).

View File

@ -1,57 +0,0 @@
// 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
var path = require('path');
var middlewares = exports;
function safeRequire(m) {
try {
return require(m);
} catch (err) {
return undefined;
}
}
function createMiddlewareNotInstalled(memberName, moduleName) {
return function() {
var msg = 'The middleware loopback.' + memberName + ' is not installed.\n' +
'Run `npm install --save ' + moduleName + '` to fix the problem.';
throw new Error(msg);
};
}
var middlewareModules = {
'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',
};
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;
middlewares.favicon = function(icon, options) {
icon = icon || path.join(__dirname, '../favicon.ico');
return favicon(icon, options);
};

View File

@ -29,7 +29,6 @@ var juggler = require('loopback-datasource-juggler');
* ``` * ```
* *
* @property {String} version Version of LoopBack framework. Static read-only property. * @property {String} version Version of LoopBack framework. Static read-only property.
* @property {String} mime
* @property {Boolean} isBrowser True if running in a browser environment; false otherwise. Static read-only property. * @property {Boolean} isBrowser True if running in a browser environment; false otherwise. Static read-only property.
* @property {Boolean} isServer True if running in a server environment; false otherwise. Static read-only property. * @property {Boolean} isServer True if running in a server environment; false otherwise. Static read-only property.
* @property {Registry} registry The global `Registry` object. * @property {Registry} registry The global `Registry` object.
@ -47,12 +46,6 @@ var loopback = module.exports = createApplication;
loopback.version = require('../package.json').version; loopback.version = require('../package.json').version;
/*!
* Expose mime.
*/
loopback.mime = express.mime;
loopback.registry = new Registry(); loopback.registry = new Registry();
Object.defineProperties(loopback, { Object.defineProperties(loopback, {
@ -128,23 +121,11 @@ function mixin(source) {
mixin(require('./runtime')); mixin(require('./runtime'));
/*! /*!
* Expose static express methods like `express.errorHandler`. * Expose static express methods like `express.Router`.
*/ */
mixin(express); mixin(express);
/*!
* Expose additional middleware like session as loopback.*
* This will keep the loopback API compatible with express 3.x
*
* ***only in node***
*/
if (loopback.isServer) {
var middlewares = require('./express-middleware');
mixin(middlewares);
}
/*! /*!
* Expose additional loopback middleware * Expose additional loopback middleware
* for example `loopback.configure` etc. * for example `loopback.configure` etc.

View File

@ -41,7 +41,6 @@
"body-parser": "^1.12.0", "body-parser": "^1.12.0",
"canonical-json": "0.0.4", "canonical-json": "0.0.4",
"continuation-local-storage": "^3.1.3", "continuation-local-storage": "^3.1.3",
"cookie-parser": "^1.3.4",
"debug": "^2.1.2", "debug": "^2.1.2",
"depd": "^1.0.0", "depd": "^1.0.0",
"ejs": "^2.3.1", "ejs": "^2.3.1",
@ -62,6 +61,7 @@
"devDependencies": { "devDependencies": {
"browserify": "^10.0.0", "browserify": "^10.0.0",
"chai": "^2.1.1", "chai": "^2.1.1",
"cookie-parser": "^1.3.4",
"es5-shim": "^4.1.0", "es5-shim": "^4.1.0",
"eslint-config-loopback": "^1.0.0", "eslint-config-loopback": "^1.0.0",
"grunt": "^0.4.5", "grunt": "^0.4.5",

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 });

View File

@ -58,19 +58,13 @@ describe('loopback', function() {
'ValidationError', 'ValidationError',
'application', 'application',
'arguments', 'arguments',
'bodyParser',
'caller', 'caller',
'compress',
'configureModel', 'configureModel',
'context', 'context',
'cookieParser',
'cookieSession',
'createContext', 'createContext',
'createDataSource', 'createDataSource',
'createModel', 'createModel',
'csrf',
'defaultDataSources', 'defaultDataSources',
'directory',
'errorHandler', 'errorHandler',
'favicon', 'favicon',
'faviconFile', 'faviconFile',
@ -80,12 +74,8 @@ describe('loopback', function() {
'getModelByType', 'getModelByType',
'isBrowser', 'isBrowser',
'isServer', 'isServer',
'json',
'length', 'length',
'logger',
'memory', 'memory',
'methodOverride',
'mime',
'modelBuilder', 'modelBuilder',
'name', 'name',
'prototype', 'prototype',
@ -94,19 +84,14 @@ describe('loopback', function() {
'remoteMethod', 'remoteMethod',
'request', 'request',
'response', 'response',
'responseTime',
'rest', 'rest',
'runInContext', 'runInContext',
'session',
'static', 'static',
'status', 'status',
'template', 'template',
'timeout',
'token', 'token',
'urlNotFound', 'urlNotFound',
'urlencoded',
'version', 'version',
'vhost',
]; ];
var actual = Object.getOwnPropertyNames(loopback); var actual = Object.getOwnPropertyNames(loopback);