Merge pull request #2394 from strongloop/remove-express-middleware

[SEMVER-MAJOR] Remove legacy express 3.x middleware getters
This commit is contained in:
Miroslav Bajtoš 2016-06-01 08:33:39 +02:00
commit 276fb5bf69
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.
## 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} mime
* @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 {Registry} registry The global `Registry` object.
@ -47,12 +46,6 @@ var loopback = module.exports = createApplication;
loopback.version = require('../package.json').version;
/*!
* Expose mime.
*/
loopback.mime = express.mime;
loopback.registry = new Registry();
Object.defineProperties(loopback, {
@ -128,23 +121,11 @@ function mixin(source) {
mixin(require('./runtime'));
/*!
* Expose static express methods like `express.errorHandler`.
* Expose static express methods like `express.Router`.
*/
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
* for example `loopback.configure` etc.

View File

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

View File

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

View File

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