Express-session - 我使用expression-session和express-mysql-session存储在数据库中

时间:2017-11-03 12:23:27

标签: node.js express-session

我正在使用expression-session和express-mysql-session进行存储        在数据库中,但每个ajax返回新的会话ID。 有关更多信息,请从http呼叫。禁用时,此问题不会发生        浏览器的安全性。

var express = require('express'); var mysql = require('mysql'); var
    jwt = require('jsonwebtoken'); var
    session=require('express-session'); var MySQLStore =
    require('express-mysql-session')(session);

    var options = { host: 'localhost',// Host name for database
    connection. port: 3306,// Port number for database connection. user:
    'root',// Database user. password: '',// Password for the above
    database user. database: 'node',// Database name.
    checkExpirationInterval: 900000,// How frequently expired sessions
    will be cleared; milliseconds. expiration: 1512671400000,// The
    maximum age of a valid session; milliseconds. createDatabaseTable:
    true,// Whether or not to create the sessions database table, if one
    does not already exist. connectionLimit: 10,// Number of connections
    when creating a connection pool schema: { tableName: 'sessions',
    columnNames: { session_id: 'session_id', expires: 'expires', data:
    'data' } } };

    var connection = mysql.createConnection(options); // or
    mysql.createPool(options); var sessionStore = new MySQLStore({}/*
    session store options */, connection); var router =
    express.Router();

    router.use(session({ name: 'session_cookie_name', secret:
    'session_cookie_secret', store: sessionStore, resave: false,
    saveUninitialized: true, cookie: { path: '/', httpOnly: false,
    secure: false, maxAge: 365 * 24 * 60 * 60 * 1000 } }));

    router.get('/session', function(req, res, next) {
    res.setHeader("Access-Control-Allow-Origin", "*");
    res.setHeader("Access-Control-Allow-Headers", "Origin,
    X-Requested-With, Content-Type, Accept"); res.send(req.sessionID);
    });
 - 

1 个答案:

答案 0 :(得分:0)

  

当我禁用浏览器的安全性时,这个问题不会出现

secure: true表示您的Cookie仅通过https doc传输。

因此,当通过http访问时,浏览器不会发送cookie,并且服务器认为它是新会话。