我使用节点js进行后端维护会话我正在使用快速会话,我的问题是我无法访问其他路由中的req.session.user,我在登录路由中调试我可以看到req。 session.user但其他路由没有req.session.user,我不确定我在哪里犯了错误。
提前致谢。
var express = require('express');
var path = require('path');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
//******SESSION Handle *//
app.use(session({
saveUninitialized:false,
resave:true,
name:"app.sid",
secret: 'hello',
cookie: {
secure: false,
path:"/",
maxAge : 60000,
},
}));
var Router = express.Router();
const cPool = require('../config/connectionPool');
const Tasks = require('./tasks');
//Login
Router.post('/login', function(req,res){
Tasks.signIn(req, res, (err,rows)=>{
if(err) return res.status(400).send({error:err.code, message:err.sqlMessage});
if(rows.length <= 0) return res.status(400).send({message:"Invalid Credentials"});
var token = jwt.sign({ id: rows[0].user_id }, 'heydonttrustme', {
expiresIn : 60*60
});
req.session.user = rows[0].user_id;
res.status(200).send({message:"Successfully Loggedin"});
});
});
app.get('/', isLoggeIn, (req, res) =>{
console.log('----', req.session);
})
const isLoggeIn = (req, res) => {
if(!req.session.user) {res.status(401).send({message: "Not authorized"}) }
else true
}