首页 > 解决方案 > 大多数中间件不再与 Express 捆绑

问题描述

我知道这是一个非常古老的问题,并且有很多关于此的帖子,但我的情况不同

错误:大多数中间件(如会话)不再与 Express 捆绑在一起,必须单独安装。

我已经设置session好了

const app = express();

app.set('trust proxy', 1) // trust first proxy
app.use(session({
  secret: 'keyboard cat',
  resave: false,
  saveUninitialized: true,
}))

但是heroku仍然向我抛出错误

这是错误的完整日志

2020-08-04T10:16:43.787603+00:00 app[api]: Initial release by user -----@hotmail.com
2020-08-04T10:16:43.787603+00:00 app[api]: Release v1 created by user -----@hotmail.com  
2020-08-04T10:16:43.931886+00:00 app[api]: Enable Logplex by user -----@hotmail.com      
2020-08-04T10:16:43.931886+00:00 app[api]: Release v2 created by user -----@hotmail.com  
2020-08-04T10:17:12.000000+00:00 app[api]: Build started by user -----@hotmail.com       
2020-08-04T10:17:31.245321+00:00 app[api]: Release v3 created by user -----@hotmail.com  
2020-08-04T10:17:31.245321+00:00 app[api]: Deploy 77531684 by user -----@hotmail.com     
2020-08-04T10:17:31.266379+00:00 app[api]: Scaled to web@1:Free by user -----@hotmail.com
2020-08-04T10:17:32.000000+00:00 app[api]: Build succeeded
2020-08-04T10:17:34.046206+00:00 heroku[web.1]: Starting process with command `node app.js`
2020-08-04T10:17:36.501981+00:00 app[web.1]: Warning: connect.session() MemoryStore is not
2020-08-04T10:17:36.502002+00:00 app[web.1]: designed for a production environment, as it will leak
2020-08-04T10:17:36.502003+00:00 app[web.1]: memory, and will not scale past a single process.
2020-08-04T10:17:36.504701+00:00 app[web.1]: /app/node_modules/express/lib/express.js:112
2020-08-04T10:17:36.504709+00:00 app[web.1]: throw new Error('Most middleware (like ' + name + ') is no longer bundled with Express and must be installed separately. Please see https://github.com/senchalabs/connect#middleware.');
2020-08-04T10:17:36.504709+00:00 app[web.1]: ^
2020-08-04T10:17:36.504710+00:00 app[web.1]:
2020-08-04T10:17:36.504712+00:00 app[web.1]: Error: Most middleware (like session) is no longer bundled with Express and must be installed separately. Please see https://github.com/senchalabs/connect#middleware.
2020-08-04T10:17:36.504713+00:00 app[web.1]: at Function.get (/app/node_modules/express/lib/express.js:112:13)
2020-08-04T10:17:36.504713+00:00 app[web.1]: at Object.<anonymous> (/app/app.js:37:17)
2020-08-04T10:17:36.504713+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:1138:30)
2020-08-04T10:17:36.504714+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
2020-08-04T10:17:36.504714+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:986:32)
2020-08-04T10:17:36.504715+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:879:14)
2020-08-04T10:17:36.504716+00:00 app[web.1]: at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
2020-08-04T10:17:36.504716+00:00 app[web.1]: at internal/main/run_main_module.js:17:47
2020-08-04T10:17:36.572852+00:00 heroku[web.1]: Process exited with status 1
2020-08-04T10:17:36.614896+00:00 heroku[web.1]: State changed from starting to crashed
2020-08-04T10:17:36.617225+00:00 heroku[web.1]: State changed from crashed to starting
2020-08-04T10:17:38.864163+00:00 heroku[web.1]: Starting process with command `node app.js`

package.json

{
  "name": "portfo",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "start": "node app.js"
  },
  "author": "Mhee",
  "license": "ISC",
  "engines": {
    "node": "12.18.0"
  },
  "dependencies": {
    "bcrypt": "^5.0.0",
    "body-parser": "^1.19.0",
    "cookie-session": "^1.4.0",
    "dotenv": "^8.2.0",
    "ejs": "^3.1.3",
    "express": "^4.17.1",
    "express-session": "^1.17.1",
    "lodash": "^4.17.19",
    "method-override": "^3.0.0",
    "mongodb": "^3.6.0",
    "mongoose": "^5.9.27",
    "passport": "^0.4.1",
    "passport-local": "^1.0.0",
    "passport-local-mongoose": "^6.0.1",
    "serve-favicon": "^2.5.0"
  }
}

已经尝试创建一个新的应用程序,但仍然得到同样的错误

我应该怎么办?

标签: expressheroku

解决方案


我建议在使用之前在 app.js 文件的顶部要求快速会话。

const session = require('express-session');

推荐阅读