1

I'm trying to deploy a dockerized node and express project.I'm getting this error and I added a memory store but the error still persists. I think my script is not in the right order

Warning: connect.session() MemoryStore is not
designed for a production environment, as it will leak
memory, and will not scale past a single process.

my script is as follows

const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const AdminBroExpress = require('admin-bro-expressjs');
const bcrypt = require('bcryptjs');
const session = require('express-session');
var MongoDBStore = require('connect-mongodb-session')(session);
const { getSecret } = require('./app/secrets');
const { adminBro } = require('./adminbro');
if (process.env.NODE_ENV !== 'production') {
    require('dotenv').config();
}
mongoose.Promise = global.Promise;
mongoose.set('useNewUrlParser', true);
mongoose.set('useFindAndModify', false);
mongoose.set('useCreateIndex', true);
mongoose.set('useUnifiedTopology', true);
const User = require('./app/models/user');
const installRoute = require('./app/routes/install');
const dataRoute =  require('./app/routes/data');
const app = express();
var store = new MongoDBStore({
  uri: getSecret('dbUri'),
  collection: 'memStoreSessions'
});
const port = process.env.PORT;
app.use(express.static('./public'));
app.set('view engine', 'ejs');
app.use(bodyParser.json());
const sess = {
  secret: 'tranquility-bay',
  cookie: {
    maxAge: 1000 * 60 * 60 * 24 * 7 // 1 week
  },
  store: store,
  resave: true,
  saveUninitialized: true
}
if (app.get('env') === 'production') {
  app.set('trust proxy', 1) // trust first proxy
  sess.cookie.secure = true // serve secure cookies
}
const router = AdminBroExpress.buildAuthenticatedRouter(adminBro, {
  authenticate: async (email, password) => {
    const user = await User.findOne({ email })
    if (user) {
      const matched = await bcrypt.compare(password, user.encryptedPassword)
      if (matched) {
        return user
      }
    }
    return false
  },
  cookiePassword: 'some-secret-password-used-to-secure-cookie',
})
app.use(session(sess));
app.use(cors());
app.options('*', cors());
app.use(adminBro.options.rootPath, router);
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use('/', installRoute);
app.use('/data', dataRoute);
const run = async () => {
  await mongoose.connect(getSecret('dbUri'),
  { useNewUrlParser: true, useUnifiedTopology: true })
  app.listen(port, () => {
    console.log(`Server running on port ${port}`)
  })
}
run()

I got tthe error before and after I added the memory store. This is a dockerized project. The app builds as expected but in tthe console I am getting this warning

webface
  • 79
  • 1
  • 4
  • Hey there! Does this solve your problem? https://stackoverflow.com/questions/44882535/warning-connect-session-memorystore-is-not-designed-for-a-production-environm – Tunmee Mar 16 '20 at 09:22
  • @Tunmee I tried about 5 different memory stores including `cookie-session` they all gave the same error. `cookie-session` took away the warning but no cookie was set. So im still stuck – webface Mar 16 '20 at 22:04

0 Answers0