快速会话在另一条路线中显示未定义

时间:2017-07-18 12:50:07

标签: node.js express express-session

我在login.js路由中创建快速会话但我无法访问student.js路由。它显示未定义。 这是我的login.js文件

const express = require('express');
const router = express.Router();
const User = require('../models/users');
router.get('/', function(req, res, next){
  res.render('login');
});
router.post('/login', function(req, res){
  let email = req.body.email;
  let password = req.body.password;
  //console.log(email +" and "+password);
  User.findOne({"email" : email, "password" : password}, function(err, user){
    if(err){
      console.log(err);
      return res.status(500).send();
    }
    if (!user) {
      return res.status(404).send();
    }
    //console.log(user.email);
    console.log("Login success");
    req.session.sess = user;
    //console.log(req.session.sess.email);
    res.send({login:true});
  })
});
router.get('/usersdata',function(req, res){
  User.find(function(err, data){
    res.send(data);
  })
})
module.exports = router;

这是我的student.js文件

const express = require('express');
const router = express.Router();

 router.get('/', function(req, res, next) {
  console.log(req.session.sess);
  if (req.session.sess) {
    res.render('student/studentHome');
  } else {
    res.render('./login');
  }
});

module.exports = router;

表达会话声明

app.use(session({
          secret: '<secret key>',
          resave: false,
          saveUninitialized: true,
          cookie: { secure: true }
        }));

如何在另一条路线中访问会话。

0 个答案:

没有答案