javascript 大多数中间件(如压缩)不再与 Express 捆绑在一起

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23102030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 00:20:15  来源:igfitidea点击:

Most middleware (like compress) is no longer bundled with Express

javascriptnode.jsexpresssails.js

提问by HP.

I just installed [email protected], [email protected], [email protected] and [email protected] (just to make sure). But I still have following error when running sails lift

我刚刚安装了[email protected][email protected][email protected][email protected](只是为了确保)。但是我在运行时仍然有以下错误sails lift

/Users/myuser/myproject/backend/node_modules/express/lib/express.js:89
      throw new Error('Most middleware (like ' + name + ') is no longer bundle
            ^
Error: Most middleware (like compress) is no longer bundled with Express and must be installed separately. Please see https://github.com/senchalabs/connect#middleware.
    at Function.Object.defineProperty.get (/Users/myuser/myproject/backend/node_modules/express/lib/express.js:89:13)
    at Object.module.exports.express.customMiddleware (/Users/myuser/myproject/backend/config/passport.js:127:20)
    at Array.loadExpress [as 1] (/usr/local/lib/node_modules/sails/lib/express/index.js:162:25)
    at listener (/usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:462:46)
    at /usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:416:17
    at Array.forEach (native)
    at _each (/usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:32:24)
    at Object.taskComplete (/usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:415:13)
    at processImmediate [as _immediateCallback] (timers.js:330:15)

Any idea why?

知道为什么吗?

采纳答案by sgress454

Sails does not officially support Express 4; there's a reason the Express dependency in the Sails package.jsonis set to an explicit version! You can use the other answers here to try and get your Sails app running, but you're on your own until we update the core to make use of the new Express.

Sails 不正式支持 Express 4;Sails 中的 Express 依赖项设置为显式版本package.json是有原因的!您可以使用此处的其他答案来尝试让您的 Sails 应用程序运行,但在我们更新核心以使用新的 Express 之前,您只能靠自己。

We are planning on reaching out to the Express team to try and synchronize releases better in the future, but at the moment we're working hard on getting out a stable release of Sails v0.10!

我们计划与 Express 团队联系,以尝试在未来更好地同步版本,但目前我们正在努力发布 Sails v0.10 的稳定版本!

回答by Jarema

It's not enough to just install those dependencies in express 4.0.

仅在 express 4.0 中安装这些依赖项是不够的。

You now need to include them in Your app with

您现在需要将它们包含在您的应用程序中

var compression = require('compression');

and then use it.

然后使用它。

There is quite nice article here: link
and also here: link

这里有很好的文章:link
和这里:link

on how to deal with migrating from expres 3.x.x to 4.x.x

关于如何处理从 expres 3.xx 到 4.xx 的迁移

回答by thefourtheye

Express 4.0's middleware documentation says,

Express 4.0 的中间件文档说

As of 4.x, Express no longer depends on Connect. All of Express' previously included middleware are now in separate repos. Please view the list of middleware. The only included middleware is now express.static().

从 4.x 开始,Express 不再依赖于 Connect。Express 以前包含的所有中间件现在都位于单独的存储库中。请查看中间件列表。现在唯一包含的中间件是 express.static()。

So, you have to explicitly include all the needed middlewares in the package.json, as a dependency. See the link in the quoted text to get to know the repository information of all the middlewares.

因此,您必须在 , 中明确包含所有需要的中间件package.json作为依赖项。查看引用文本中的链接,了解所有中间件的存储库信息。

回答by YinPeng.Wei

https://github.com/expressjs/body-parser/issues/31change your bodyParser()to app.use(bodyParser.json()) app.use(bodyParser.urlencoded({ extended: true }))Maybe you'll get the right answer. since express version 3 to 4, body-parser is not in express,you should install it independencly. May help you.

https://github.com/expressjs/body-parser/issues/31将您的更改bodyParser()app.use(bodyParser.json()) app.use(bodyParser.urlencoded({ extended: true }))也许您会得到正确的答案。由于 express 版本 3 到 4,body-parser 不在 express 中,您应该独立安装它。可以帮到你。