首页 > 解决方案 > Cannot deploy MERN Stack app on Heroku properly(session is not designd for a production enviorment)

问题描述

I'm having a hard time deploying my fullstack mern app to heroku, I've tried using the express session down below with no success: https://expressjs.com/en/resources/middleware/session.html

I keep getting this error Warning: connect.session() MemoryStore is not designed for a production environment, as it will leak

Code:

const session = require("express-session");
const MongoStore = require('connect-mongo');

app.set('trust proxy', 1) // trust first proxy

app.use(session({
  secret: 'secret',
  resave: false,
  saveUninitialized: true,
  cookie: { secure: true },
  store: MongoStore.create({ mongoUrl: 'process.env.MONGOURI_ENV' })
}))
app.use((req,res,next)=>{
  if(!req.session){
      return next(new Error('Oh no')) //handle error
  }
  next() //otherwise continue
  });

Full heroku log:

2021-08-31T18:49:43.207434+00:00 app[api]: Release v1 created by user matan2288@gmail.com
2021-08-31T18:49:43.207434+00:00 app[api]: Initial release by user matan2288@gmail.com
2021-08-31T18:49:43.458553+00:00 app[api]: Release v2 created by user matan2288@gmail.com
2021-08-31T18:49:43.458553+00:00 app[api]: Enable Logplex by user matan2288@gmail.com
2021-08-31T18:50:07.354005+00:00 app[api]: Set mongoURI config vars by user matan2288@gmail.com
2021-08-31T18:50:07.354005+00:00 app[api]: Release v3 created by user matan2288@gmail.com
2021-08-31T18:50:22.969103+00:00 heroku[router]: at=info code=H81 desc="Blank app" method=GET path="/" host=banking-app-mern5323212.herokuapp.com request_id=e4dbf26b-610a-490e-8c34-4df1f1d07856 fwd="147.236.153.118" dyno= connect= service= status=502 bytes= protocol=https
2021-08-31T18:50:24.030589+00:00 heroku[router]: at=info code=H81 desc="Blank app" method=GET path="/favicon.ico" host=banking-app-mern5323212.herokuapp.com request_id=2dbddc6e-29c7-465f-a729-74b13c756629 fwd="147.236.153.118" dyno= connect= service= status=502 bytes= protocol=https
2021-08-31T18:51:33.000000+00:00 app[api]: Build started by user matan2288@gmail.com
2021-08-31T18:53:48.104100+00:00 app[api]: Release v4 created by user matan2288@gmail.com
2021-08-31T18:53:48.104100+00:00 app[api]: Deploy 1c82cc7f by user matan2288@gmail.com
2021-08-31T18:53:48.121684+00:00 app[api]: Scaled to web@1:Free by user matan2288@gmail.com
2021-08-31T18:53:49.000000+00:00 app[api]: Build succeeded
2021-08-31T18:53:55.211984+00:00 heroku[web.1]: Starting process with command `npm start`
2021-08-31T18:53:57.295647+00:00 app[web.1]:
2021-08-31T18:53:57.295671+00:00 app[web.1]: > server@1.0.0 start /app
2021-08-31T18:53:57.295671+00:00 app[web.1]: > node server.js
2021-08-31T18:53:57.295671+00:00 app[web.1]:
2021-08-31T18:53:57.699413+00:00 app[web.1]: WARNING: NODE_ENV value of 'production' did not match any deployment config file names. 
2021-08-31T18:53:57.699653+00:00 app[web.1]: WARNING: See https://github.com/lorenwest/node-config/wiki/Strict-Mode
2021-08-31T18:53:57.704352+00:00 app[web.1]: Warning: connect.session() MemoryStore is not
2021-08-31T18:53:57.704355+00:00 app[web.1]: designed for a production environment, as it will leak
2021-08-31T18:53:57.704355+00:00 app[web.1]: memory, and will not scale past a single process.
2021-08-31T18:53:57.708402+00:00 app[web.1]: Example app listening at http://localhost:39030
2021-08-31T18:53:57.741346+00:00 app[web.1]: (node:22) Warning: Setting the NODE_TLS_REJECT_UNAUTHORIZED environment variable to '0' 
makes TLS connections and HTTPS requests insecure by disabling certificate verification.
2021-08-31T18:53:57.741348+00:00 app[web.1]: (Use `node --trace-warnings ...` to show where the warning was created)
2021-08-31T18:53:58.432611+00:00 app[web.1]: Mongo DB connected
2021-08-31T18:53:58.938680+00:00 heroku[web.1]: State changed from starting to up
2021-08-31T18:54:00.925068+00:00 app[web.1]: NotFoundError: Not Found
2021-08-31T18:54:00.925078+00:00 app[web.1]: at /app/server.js:74:8
2021-08-31T18:54:00.925079+00:00 app[web.1]: at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)2021-08-31T18:54:00.925079+00:00 app[web.1]: at trim_prefix (/app/node_modules/express/lib/router/index.js:317:13)
2021-08-31T18:54:00.925080+00:00 app[web.1]: at /app/node_modules/express/lib/router/index.js:284:7
2021-08-31T18:54:00.925080+00:00 app[web.1]: at Function.process_params (/app/node_modules/express/lib/router/index.js:335:12)       
2021-08-31T18:54:00.925081+00:00 app[web.1]: at next (/app/node_modules/express/lib/router/index.js:275:10)
2021-08-31T18:54:00.925082+00:00 app[web.1]: at SessionStrategy.strategy.pass (/app/node_modules/passport/lib/middleware/authenticate.js:343:9)
2021-08-31T18:54:00.925082+00:00 app[web.1]: at SessionStrategy.authenticate (/app/node_modules/passport/lib/strategies/session.js:75:10)
2021-08-31T18:54:00.925082+00:00 app[web.1]: at attempt (/app/node_modules/passport/lib/middleware/authenticate.js:366:16)
2021-08-31T18:54:00.925083+00:00 app[web.1]: at authenticate (/app/node_modules/passport/lib/middleware/authenticate.js:367:7)       
2021-08-31T18:54:00.925083+00:00 app[web.1]: at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)2021-08-31T18:54:00.925083+00:00 app[web.1]: at trim_prefix (/app/node_modules/express/lib/router/index.js:317:13)
2021-08-31T18:54:00.925084+00:00 app[web.1]: at /app/node_modules/express/lib/router/index.js:284:7
2021-08-31T18:54:00.925084+00:00 app[web.1]: at Function.process_params (/app/node_modules/express/lib/router/index.js:335:12)       
2021-08-31T18:54:00.925085+00:00 app[web.1]: at next (/app/node_modules/express/lib/router/index.js:275:10)
2021-08-31T18:54:00.925085+00:00 app[web.1]: at initialize (/app/node_modules/passport/lib/middleware/initialize.js:53:5)
2021-08-31T18:54:00.925269+00:00 heroku[router]: at=info method=GET path="/" host=banking-app-mern5323212.herokuapp.com request_id=7cc30e95-dd8e-4639-b9b0-eedfae02690b fwd="147.236.153.118" dyno=web.1 connect=0ms service=13ms status=404 bytes=618 protocol=https     
2021-08-31T18:54:01.729030+00:00 app[web.1]: NotFoundError: Not Found
2021-08-31T18:54:01.729061+00:00 app[web.1]: at /app/server.js:74:8
2021-08-31T18:54:01.729065+00:00 app[web.1]: at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)2021-08-31T18:54:01.729066+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=banking-app-mern5323212.herokuapp.com request_id=77cb9465-33d8-4ae4-b8cf-bf9df4198624 fwd="147.236.153.118" dyno=web.1 connect=0ms service=3ms status=404 bytes=486 protocol=https
2021-08-31T18:54:01.729066+00:00 app[web.1]: at trim_prefix (/app/node_modules/express/lib/router/index.js:317:13)
2021-08-31T18:54:01.729067+00:00 app[web.1]: at /app/node_modules/express/lib/router/index.js:284:7
2021-08-31T18:54:01.729067+00:00 app[web.1]: at Function.process_params (/app/node_modules/express/lib/router/index.js:335:12)       
2021-08-31T18:54:01.729067+00:00 app[web.1]: at next (/app/node_modules/express/lib/router/index.js:275:10)
2021-08-31T18:54:01.729069+00:00 app[web.1]: at SessionStrategy.strategy.pass (/app/node_modules/passport/lib/middleware/authenticate.js:343:9)
2021-08-31T18:54:01.729069+00:00 app[web.1]: at SessionStrategy.authenticate (/app/node_modules/passport/lib/strategies/session.js:75:10)
2021-08-31T18:54:01.729069+00:00 app[web.1]: at attempt (/app/node_modules/passport/lib/middleware/authenticate.js:366:16)
2021-08-31T18:54:01.729070+00:00 app[web.1]: at authenticate (/app/node_modules/passport/lib/middleware/authenticate.js:367:7)       
2021-08-31T18:54:01.729070+00:00 app[web.1]: at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)2021-08-31T18:54:01.729070+00:00 app[web.1]: at trim_prefix (/app/node_modules/express/lib/router/index.js:317:13)
2021-08-31T18:54:01.729071+00:00 app[web.1]: at /app/node_modules/express/lib/router/index.js:284:7
2021-08-31T18:54:01.729071+00:00 app[web.1]: at Function.process_params (/app/node_modules/express/lib/router/index.js:335:12)       
2021-08-31T18:54:01.729072+00:00 app[web.1]: at next (/app/node_modules/express/lib/router/index.js:275:10)
2021-08-31T18:54:01.729072+00:00 app[web.1]: at initialize (/app/node_modules/passport/lib/middleware/initialize.js:53:5)
2021-08-31T19:08:04.000000+00:00 app[api]: Build started by user matan2288@gmail.com
2021-08-31T19:10:11.681977+00:00 app[api]: Release v5 created by user matan2288@gmail.com
2021-08-31T19:10:11.681977+00:00 app[api]: Deploy a4487bd0 by user matan2288@gmail.com
2021-08-31T19:10:12.000000+00:00 app[api]: Build succeeded
2021-08-31T19:10:12.828104+00:00 heroku[web.1]: Restarting
2021-08-31T19:10:12.902334+00:00 heroku[web.1]: State changed from up to starting
2021-08-31T19:10:13.683316+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2021-08-31T19:10:13.782947+00:00 heroku[web.1]: Process exited with status 143
2021-08-31T19:10:19.680005+00:00 heroku[web.1]: Starting process with command `npm start`
2021-08-31T19:10:21.834967+00:00 app[web.1]:
2021-08-31T19:10:21.834982+00:00 app[web.1]: > server@1.0.0 start /app
2021-08-31T19:10:21.834982+00:00 app[web.1]: > node server.js
2021-08-31T19:10:21.834983+00:00 app[web.1]:
2021-08-31T19:10:22.446343+00:00 app[web.1]: WARNING: NODE_ENV value of 'production' did not match any deployment config file names. 
2021-08-31T19:10:22.446774+00:00 app[web.1]: WARNING: See https://github.com/lorenwest/node-config/wiki/Strict-Mode
2021-08-31T19:10:22.458769+00:00 app[web.1]: Example app listening at http://localhost:4307
2021-08-31T19:10:22.460178+00:00 app[web.1]: (node:22) UnhandledPromiseRejectionWarning: MongoParseError: Invalid connection string "process.env.MONGOURI_ENV"
2021-08-31T19:10:22.460180+00:00 app[web.1]: at new ConnectionString (/app/node_modules/mongodb-connection-string-url/lib/index.js:66:19)
2021-08-31T19:10:22.460180+00:00 app[web.1]: at Object.parseOptions (/app/node_modules/connect-mongo/node_modules/mongodb/lib/connection_string.js:209:17)
2021-08-31T19:10:22.460181+00:00 app[web.1]: at new MongoClient (/app/node_modules/connect-mongo/node_modules/mongodb/lib/mongo_client.js:62:46)
2021-08-31T19:10:22.460181+00:00 app[web.1]: at Function.connect (/app/node_modules/connect-mongo/node_modules/mongodb/lib/mongo_client.js:184:33)
2021-08-31T19:10:22.460182+00:00 app[web.1]: at new MongoStore (/app/node_modules/connect-mongo/build/main/lib/MongoStore.js:110:46) 
2021-08-31T19:10:22.460182+00:00 app[web.1]: at Function.create (/app/node_modules/connect-mongo/build/main/lib/MongoStore.js:136:16)2021-08-31T19:10:22.460183+00:00 app[web.1]: at Object.<anonymous> (/app/server.js:44:21)
2021-08-31T19:10:22.460184+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:1068:30)
2021-08-31T19:10:22.460184+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
2021-08-31T19:10:22.460185+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:933:32)
2021-08-31T19:10:22.460185+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:774:14)
2021-08-31T19:10:22.460186+00:00 app[web.1]: at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)     
2021-08-31T19:10:22.460186+00:00 app[web.1]: at internal/main/run_main_module.js:17:47
2021-08-31T19:10:22.460186+00:00 app[web.1]: (Use `node --trace-warnings ...` to show where the warning was created)
2021-08-31T19:10:22.460297+00:00 app[web.1]: (node:22) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
2021-08-31T19:10:22.460358+00:00 app[web.1]: (node:22) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In 
the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
2021-08-31T19:10:22.495811+00:00 app[web.1]: (node:22) Warning: Setting the NODE_TLS_REJECT_UNAUTHORIZED environment variable to '0' 
makes TLS connections and HTTPS requests insecure by disabling certificate verification.
2021-08-31T19:10:23.175208+00:00 app[web.1]: Mongo DB connected
2021-08-31T19:10:23.372609+00:00 heroku[web.1]: State changed from starting to up

标签: node.jsmongodbexpresssessionheroku

解决方案


推荐阅读