diff --git a/3.0-RELEASE-NOTES.md b/3.0-RELEASE-NOTES.md index b9aa90a4..f3fad7ad 100644 --- a/3.0-RELEASE-NOTES.md +++ b/3.0-RELEASE-NOTES.md @@ -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). diff --git a/lib/express-middleware.js b/lib/express-middleware.js deleted file mode 100644 index 15a22410..00000000 --- a/lib/express-middleware.js +++ /dev/null @@ -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); -}; diff --git a/lib/loopback.js b/lib/loopback.js index 386551bc..79bc19aa 100644 --- a/lib/loopback.js +++ b/lib/loopback.js @@ -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. diff --git a/package.json b/package.json index 7c118d8d..53d7e6c9 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/server/middleware/favicon.js b/server/middleware/favicon.js index c694009a..3081698b 100644 --- a/server/middleware/favicon.js +++ b/server/middleware/favicon.js @@ -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); +}; diff --git a/test/access-token.test.js b/test/access-token.test.js index bca91d4e..eeb5b794 100644 --- a/test/access-token.test.js +++ b/test/access-token.test.js @@ -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 }); diff --git a/test/loopback.test.js b/test/loopback.test.js index 3f97d6d5..01a7c41d 100644 --- a/test/loopback.test.js +++ b/test/loopback.test.js @@ -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);