title: Node.js - Express session data not persisting
body: <p>login works as expected and session data returned but beyond that I am unable to get session data to persist. For example, when I call getUser (below) the session data is not returned.</p>
  
  <p><strong>app.js (setup)</strong></p>
  
  <pre><code>const express = require('express')
  const bodyParser = require('body-parser')
  const cors = require('cors')
  const morgan = require('morgan')
  const request = require('request')
  var session = require('express-session');
  var MongoStore = require('connect-mongo')(session);
  
  const app = express()
  app.use(morgan('combined'))
  app.use(bodyParser.json())
  app.use(cors({
      credentials: true
  }))
  
  const mongodb_conn_module = require('./mongodbConnModule');
  var db = mongodb_conn_module.connect();
  
  app.use(session({
      secret: 'work hard',
      resave: false,
      saveUninitialized: true,
      cookie: {
          secure: false,
          maxAge: 24 * 60 * 60 * 1000
      },
      store: new MongoStore({
          mongooseConnection: db,
          ttl: 24 * 60 * 60 // Keeps session open for 1 day
      })
  }));
  </code></pre>
  
  <p><strong>app.js (login)</strong></p>
  
  <pre><code>app.post('/login', (req, res) =&gt; {
      if (req.body.logEmail &amp;&amp; req.body.logPassword) {
          Users.authenticate(req.body.logEmail, req.body.logPassword, function (error, user) {
              if (error || !user) {
                  var err = new Error('Wrong email or password.');
                  err.status = 401;
                  res.send("Wrong email or password.");
              } else {
                  req.session.userId = user._id;
                  res.send({
                      success: true,
                      sessionInfo: req.session
                  });
              }
          });
      } else {
          var err = new Error('All fields required.');
          err.status = 400;
          res.send("All fields required.");
      }
  })
  </code></pre>
  
  <p>which returns session data (userId)
  <a href="https://i.stack.imgur.com/IhJVH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IhJVH.png" alt="enter image description here"></a></p>
  
  <p><strong>app.js (getUser)</strong></p>
  
  <pre><code>app.get('/get_user', (req, res) =&gt; {
      res.send(req.session)
  })
  </code></pre>
  
  <p>which appears to drop session data
  <a href="https://i.stack.imgur.com/f8iUA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/f8iUA.png" alt="enter image description here"></a></p>
  
  <p><strong>UsersService.js (model)</strong></p>
  
  <pre><code>var mongoose = require("mongoose");
  var bcrypt = require('bcrypt');
  var Schema = mongoose.Schema;
  
  var UserSchema = new Schema({
      firstName: {
          type: String,
          required: true
      },
      lastName: {
          type: String,
          required: true
      },
      userName: {
          type: String,
          unique: true,
          required: true,
          trim: true
      },
      email: {
          type: String,
          unique: true,
          required: true,
          trim: true
      },
      password: {
          type: String,
          required: true,
      },
      passwordConf: {
          type: String,
          required: true,
      }
  });
  
  //authenticate input against database
  UserSchema.statics.authenticate = function (email, password, callback) {
      Users.findOne({ email: email })
          .exec(function (err, user) {
              if (err) {
                  return callback(err)
              } else if (!user) {
                  var err = new Error('User not found.');
                  err.status = 401;
                  return callback(err);
              }
          bcrypt.compare(password, user.password, function (err, result) {
              if (result === true) {
                  return callback(null, user);
              } else {
                  return callback();
              }
          })
      });
  }
  
  // hashing a password before saving it to the database
  UserSchema.pre('save', function (next) {
      var user = this;
      bcrypt.hash(user.password, 10, function (err, hash) {
          if (err) {
              return next(err);
          }
          user.password = hash;
          next();
      })
  });
  
  var Users = mongoose.model("Users", UserSchema);
  module.exports = Users;
  </code></pre>
  
  <p>Thanks in advance!</p>