title: req.session is undefined express-session
body: <p>I know this question has been asked several times. However, none of the answers I've read has solved my question.</p>
  <p>Many of them were outdated for express v4, as express-session is currently a separate module. Those question/answers are here:</p>
  <ul>
  <li><a href="https://stackoverflow.com/questions/10191692/session-is-undefined-when-using-express-redis-for-session-store">'session' is undefined when using express / redis for session store</a></li>
  <li><a href="https://stackoverflow.com/questions/19608366/req-session-undefined-in-express-js">req.session undefined in Express.js</a></li>
  <li><a href="https://stackoverflow.com/questions/22489237/nodejs-express-req-session-undefined">nodejs express req.session undefined</a></li>
  </ul>
  <p>Other questions are updated, however the solution given doesn't fix my problem. Those question/answers are here:</p>
  <ul>
  <li><a href="https://stackoverflow.com/questions/37138225/nodejs-express-session-req-session-is-undefined">NodeJS express-session req.session is undefined</a></li>
  <li><a href="https://stackoverflow.com/questions/39796228/req-session-is-undefined-using-express-session">req.session is undefined using express-session</a></li>
  </ul>
  <p>Most of the solutions are the sequence of middlewares when configuring the app. I've tried different options and I doesn't find the correct way to do it. Maybe it's just that.</p>
  <p>In other solution, someone says that <code>session</code> middleware cannot be called, because if it were, <code>req.session</code> would be defined. I've used middlewares just before an after <code>app.use(session({...}))</code>, and checked that both get called.</p>
  <p>I've also found in <a href="https://github.com/expressjs/session/issues/4" rel="nofollow noreferrer">this issue</a> someone saying that he gets <code>req.session</code> undefined when the store disconnects. I'm using the default store so far. Could this be the problem?</p>
  <p>I'm getting this error:</p>
  <blockquote>
  <p>TypeError: Cannot read property 'destroy' of undefined</p>
  </blockquote>
  <p>It breaks just when I try to logout a session with <code>req.session.destroy()</code> at login.js (see code below)</p>
  <br>
  <h3>My code</h3>
  <p><em><strong>server.js</strong></em></p>
  <pre><code>const express = require('express');
  const session = require('express-session');
  const mongo = require('mongodb');
  const passport = require('passport');
  const path = require('path');
  const routes = require('./app/routes');
  const login = require('./app/routes/login.js');
  
  const app = express();
  app.use(session({
    secret: 'key',
    resave: false,
    saveUninitialized: true,
  }));
  app.use(passport.initialize());
  app.use(passport.session());
  app.use('/public', express.static(path.normalize('./public')));
  app.use(routes);
  app.use(login);
  app.set('view engine', 'pug');
  
  const MongoClient = mongo.MongoClient;
  MongoClient.connect('mongodb://localhost:27017/myapp')
  .then((db) =&gt; {
    // Link the database through the app. It will be available in the req.app object
    app.db = db;
    console.log('App listening on port ' + process.env.PORT);
    app.listen(process.env.PORT);
  })
  .catch((err) =&gt; {
    console.log('There was an error connecting to the database.');
    console.log(err);
  });
  
  module.exports = app; // For testing
  </code></pre>
  <p><em><strong>login.js</strong></em></p>
  <pre><code>const express = require('express');
  const passport = require('passport');
  const router = new express.Router();
  
  // Other login logic here
  
  router.get('/logout', (res, req) =&gt; {
    req.session.destroy((err) =&gt; {
      req.redirect('/');
    });
  });
  
  module.exports = router;
  </code></pre>