Node.js不保存用户会话

时间:2017-05-06 16:41:32

标签: javascript node.js session cookies

我在构建会话系统时使用了nodejs,但由于某些原因,这根本不起作用,我无法弄清楚原因,我不知道我是否遗漏了什么。

这是代码(我还在测试一些东西,所以代码根本不理想,但应该在会话中起作用):

var express = require('express');
var app = require('express')();
var io_session = require("express-socket.io-session");
var e_session = require("express-session");

var MemoryStore = e_session.MemoryStore;

var enableCORS = function(req, res, next) {

    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
    res.header('Access-Control-Allow-Headers', 'Origin, Accept, Content-Type, Authorization, Content-Length, X-Requested-With, *');

        // intercept OPTIONS method
    if ('OPTIONS' == req.method) {
        res.sendStatus(200);
    } else {
        next();
    };
};

var e_session = e_session({
    secret: "eaifgneaifiea30r3jafeaofoeakfoeak",
    store: new MemoryStore(),
    resave: false,
    saveUninitialized: true,
    cookie: { 
        secure: false,
        maxAge: 24*60*60*1000 //1 minute
   } 
});


app.use(function(req, res, next) {      
  ///console.log('Current User:', req);
  next();
});

app.use(e_session);
app.use(enableCORS);

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.use(morgan('dev'));

app.post("/TEST_start_login",function(req,res){

    console.log("starting login");

    req.session.alias = ["lol"];
    req.session.user = "WTF";

    console.log("req.session:",req.session);

    to_send = JSON.stringify(returnValues("success","login_registered",{}));

    res.send(to_send);

});

app.post("/TEST_test_login",function(req,res){

    console.log("SESSION:",req.session);
    res.status(200).send();

});

正如您在前两种方法中所看到的,我将两个变量保存到会话中,以便我可以测试用户是否已登录。

这是我在前端测试的javascript:

function sendToServer (object,type,callback,url) {

    if(url == undefined) {
        url = "";
    }

    if(typeof type == undefined) {
        type = "POST";
    }

     $.ajax({
                url: 'http://localhost:8080'+url,
                type: type,
                contentType: 'application/json; charset=utf-8',
                data: JSON.stringify(object),
                success: callback

            });
}

function login() {

    var data = {}

    sendToServer(data,"POST",function(data) {

        //return
        data = JSON.parse(data);

        console.log("returned from login:",data);

    },"/TEST_start_login");

}

function checkSession() {

    var data = {}

    sendToServer(data,"POST",function(data) {

        console.log("returned from login:",data);

    },"/TEST_test_login");

}

这是执行logincheckSession的服务器的输出:

登录:

  

req.session:Session {cookie:{path:'/',        _expires:2017-05-07T16:32:43.377Z,        originalMaxAge:86400000,        httpOnly:是的,        安全:假},别名:['lol'],用户:'WTF'}

变量'别名'和'用户'在会话中设置。

check_session:

  

会话:会话{cookie:{路径:'/',        _expires:2017-05-07T16:32:45.547Z,        originalMaxAge:86400000,        httpOnly:是的,        安全:假}}

如您所见,在nodejs中通过'TEST_test_login'路线不再显示变量......

我真的不明白为什么会这样。有人可以帮我搞清楚吗?

非常感谢

0 个答案:

没有答案