Warning: connect.session () MemoryStore is not intended for production environments, as this will lead to a memory leak and will not be scaled in one process

Good afternoon guys, here is my code:

var express = require('express'); var session = require('express-session'); var app = express(); app.set('trust proxy', 1); app.use(session({ secret: 'secret', saveUninitialized: true, resave: false, maxAge: 1000 * 60 * 15, cookie:{ secure: true } })); 

this code always returns to my a log:

Warning: connect.session () MemoryStore is not intended for a production environment, as this will lead to a memory leak and will not be scaled in one process.

I tried to take a walk about this, but it seems that I do not understand some lessons. Sorry for my bad english.

+9
javascript memory-leaks cookies session
source share
2 answers

I hope this helps someone who is trying to solve the same problem as mine. Just dug it myself.

 //-momery unleaked--------- app.set('trust proxy', 1); app.use(session({ cookie:{ secure: true, maxAge:60000 }, store: new RedisStore(), secret: 'secret', saveUninitialized: true, resave: false })); app.use(function(req,res,next){ if(!req.session){ return next(new Error('Oh no')) //handle error } next() //otherwise continue }); 
+3
source share

It's all about storing sessions; you must add a storage system that stores sessions in your database. This will help your application manage sessions.

For example, in mongodb you can use connect-mongo, you must find the store package for other databases as well.

https://www.npmjs.com/package/connect-mongo

 const session = require('express-session'); const MongoStore = require('connect-mongo')(session); app.use(session({ secret: 'foo', store: new MongoStore(options) })); 
+1
source share

All Articles