title: Can not save session with Vue + Express
body: <p>I have server side with ExpressJS with Vue in frontend. I wanted to save sessions, but I can not do this with Vue frontend. I can not find solution of this/ Server side is:</p>
  
  <pre><code>    let express = require('express');
  
  let path = require('path');
  let favicon = require('serve-favicon');
  let logger = require('morgan');
  let cookieParser = require('cookie-parser');
  let bodyParser = require('body-parser');
  
  
  var session = require('express-session')
  // const MongoStore = require('connect-mongo')(express_session);
  
  let index = require('./src/routes/index');
  let users = require('./src/routes/users');
  let gallery = require('./src/routes/gallery')
  let auth = require('./src/routes/auth')
  let contacts = require('./src/routes/contacts')
  let services = require('./src/routes/services')
  
  
  let cors = require('cors')
  
  let app = express();
  
  app.use(cors({
     origin:['http://localhost:8080'],
      methods:['GET','POST'],
      credentials: true // enable set cookie
  }))
  
  
  let mongoose = require('mongoose')
  
  
  let express_session = require("express-session");
  const MongoStore = require('connect-mongo')(express_session);
  
  
  
  
  //авторизация
  app.use(session({
      secret: 'keyboard',
       resave: false,
       saveUninitialized: true,
      store: new MongoStore({
          url: 'mongodb://localhost/studio'
      })
  }))
  
  
  
  let mongoDB = 'mongodb://127.0.0.1/studio';
  mongoose.connect(mongoDB, {
      useMongoClient: true
  });
  
  let db = mongoose.connection;
  
  db.on('error', console.error.bind(console, 'MongoDB connection error:'));
  
  // view engine setup
  app.set('views', path.join(__dirname, 'views'));
  app.set('view engine', 'pug');
  
  // uncomment after placing your favicon in /public
  //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
  app.use(logger('dev'));
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({ extended: false }));
  
  
  //app.use(cookieParser());
  app.use(express.static(path.join(__dirname, 'public')));
  
  app.use('/', index);
  app.use('/users', users);
  app.use('/gallery', gallery );
  app.use('/contacts', contacts );
  app.use('/auth', auth);
  app.use('/services', services );
  
  
  //Авторизация в данной система
  
  
  // catch 404 and forward to error handler
  app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
  });
  
  
  // error handler
  app.use(function(err, req, res, next) {
    // set locals, only providing error in development
    res.locals.message = err.message;
    res.locals.error = req.app.get('env') === 'development' ? err : {};
  
    // render the error page
    res.status(err.status || 500);
    res.render('error');
  });
  
  //app.listen(3002);
  
  
  module.exports = app;
  </code></pre>
  
  <p>I save sessions in mongoose, and when i check server side session is working. But when I want to save session in Vue side it does not working. I think it was because of cors, but it did not help.
   Code of vue here:</p>
  
  <pre><code>&lt;template&gt;
    &lt;div class="hello"&gt;
      &lt;h1&gt;{{ msg }}&lt;/h1&gt;
      &lt;h2&gt;Essential Links&lt;/h2&gt;
      &lt;ul&gt;
        &lt;li&gt;&lt;a href="https://vuejs.org" target="_blank"&gt;Core Docs&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href="https://forum.vuejs.org" target="_blank"&gt;Forum&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href="https://chat.vuejs.org" target="_blank"&gt;Community Chat&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href="https://twitter.com/vuejs" target="_blank"&gt;Twitter&lt;/a&gt;&lt;/li&gt;
        &lt;br&gt;
        &lt;li&gt;&lt;a href="http://vuejs-templates.github.io/webpack/" target="_blank"&gt;Docs for This Template&lt;/a&gt;&lt;/li&gt;
      &lt;/ul&gt;
      &lt;h2&gt;Ecosystem&lt;/h2&gt;
      &lt;ul&gt;
        &lt;li&gt;&lt;a href="http://router.vuejs.org/" target="_blank"&gt;vue-router&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href="http://vuex.vuejs.org/" target="_blank"&gt;vuex&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href="http://vue-loader.vuejs.org/" target="_blank"&gt;vue-loader&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href="https://github.com/vuejs/awesome-vue" target="_blank"&gt;awesome-vue&lt;/a&gt;&lt;/li&gt;
      &lt;/ul&gt;
    &lt;/div&gt;
  &lt;/template&gt;
  
  &lt;script&gt;
  export default {
    name: 'HelloWorld',
    data () {
      return {
        msg: 'Welcome to Your Vue.js App',
        page_views:undefined,
      }
    },
    beforeMount(){
      this.$http.get("http://localhost:8120/").then((res)=&gt;{
          if(!res.body.session){
            console.log("Session not exist "+res.body.message)
            this.msg = res.body.message;
          }
          else{
            console.log("Session exist "+res.body.page_views)
            this.msg  = res.body.page_views
          }
      },(err)=&gt;{
        console.log(err)
      })
    }
  }
  &lt;/script&gt;
  </code></pre>
  
  <p>And code of endpoint here is:</p>
  
  <pre><code>var express = require('express');
  var router = express.Router();
  
  let cors = require('cors')
  router.use(cors())
  
  /* GET home page. */
  router.get('/', function(req, res){
     if(req.session.page_views){
        req.session.page_views++;
        res.json({session:true,page_views:req.session.page_views});
     } else {
        req.session.page_views = 1;
        res.json({session:false, message:"Welcome to this page for the first time!"})
     }
  });
  
  
  module.exports = router;
  </code></pre>