Express.js会话约3分钟后丢失 [英] Express.js session lost after about 3min

查看:43
本文介绍了Express.js会话约3分钟后丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用express.js和React.成功登录后,我将 user_id 存储在会话中,但是2-3分钟后,会话丢失了,当我刷新页面时,他们会将我注销.这是我的server.js

I use express.js and React. After success login I store user_id in session but after 2-3min session is lost and when I refresh page they log out me. Here is my server.js

var mysql = require('mysql');
var connection  = mysql.createConnection({
  host     : 'host',
  user     : 'user',
  password : 'password',
  database : 'database',
  pool     : { maxConnections: 50, maxIdleTime: 30},
});

connection.connect(function(error){
    if(!!error){
        console.log('error');
    }else{
        console.log('Connected');
    }
})
//pluginy
var bodyParser = require('body-parser');
var express = require('express');
var session = require('express-session');
var app = express();
//ustwienia
app.use(bodyParser()); //przesylanie danych w req.body
app.set('trust proxy', 1) // trust first proxy
app.use(session({
    secret: 'v8bRRj7XVC6Dvp',
    saveUninitialized: true,
    resave: true,
    cookie: {secure: true}
}));
app.use('/static', express.static(__dirname + '/src'));
app.use('/dist', express.static(__dirname + '/dist'));
app.use('/php', express.static(__dirname + '/php'));
app.set('views', __dirname + '/views');
app.set('view engine','pug');

function checkAuth(req, res, next) {
    console.log('User id: '+req.session.user_id);

  if (!req.session.user_id) {
      if(req.route.path=='/'){
          res.render("indexNotLogin",{title:''});
      }else{
        res.send('You are not authorized to view this page');
    }
  } else {
    next();
  }
}

//roots
    //index
    app.get('/', checkAuth, function(req, res){
        res.render("index",{title:''});
    })
    //funkcje
    app.get('/funkcje', function(req, res){
        res.render("funkcje",{title:''});
    })

//trylogin
    app.post('/trylogin', function(req, res){
        var username = req.body.name;
        var password = req.body.password;
        connection.query("SELECT * FROM user WHERE username='"+username+"' AND pass='"+password+"' LIMIT 1", function(error, rows,fields){
            //callback
            if(!!error){
                console.log('error in query');
            }else{
                if(rows.length){
                    console.log(rows[0].id)
                    req.session.user_id = rows[0].id;
                    res.redirect('/');
                }else{
                    res.send('user dont exist')
                }
            }
        })

    })
app.listen(3000,'0.0.0.0', function(){
    console.log('Port: 3000')
})

提交表单后,我可以执行/trylogin 的功能,并且可以正常工作, req.session.user_id = rows [0] .id 是user_id,但是为什么会丢失会话这么快?

after form submit I do function /trylogin and eveything work fine, req.session.user_id = rows[0].id is user_id, but why session is lost so fast ?

推荐答案

您可以通过在会话中间件中使用 maxAge 选项来增加会话时间:

You can increase your session time by using maxAge option in session middleware:

app.use(session({
    secret: 'v8bRRj7XVC6Dvp',
    saveUninitialized: true,
    resave: true,
    cookie: {maxAge:900000} //here ,15 min session time
}));

这篇关于Express.js会话约3分钟后丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆