开发者

NodeJS session not persisted/retrieved

开发者 https://www.devze.com 2023-04-12 19:30 出处:网络
I\'m trying to do some session authentication with node. This piece of code works fine when I run it on my localhost but the session is not retrieved when on a remote server.

I'm trying to do some session authentication with node. This piece of code works fine when I run it on my localhost but the session is not retrieved when on a remote server.

Every time I hit the remote server, I can see that a new session id is generated whereas it stays the same on localhost.

Note that I'm accessing the URL directly with the browser through an ip address (no client side JS). The problem originally appeared with expressJS/jquery but I managed to reproduce with this following code:

var express = require('express');
var app = express.createServer();

app.configure(function(){
    app.use(express.bodyParser());
    app.use(express.cookieParser());
    app.use(express.session({ secret: "keyboard cat开发者_运维知识库" }));
    app.use(app.router);
});

app.get('/login', function(req, res){
    // Perhaps we posted several items with a form
    // (use the bodyParser() middleware for this)
    req.session.loggedIn = true;
    var msg = "You are logged In. SessionId:"+req.session.id;
    console.log(msg);
    res.end(msg);
});

app.get('/isLoggedIn', function(req, res){
    var msg = "isLoggedIn: "+req.session.loggedIn+" SessionId:"+req.session.id;
    console.log(msg);
    res.end(msg);
});

app.get('/logout', function(req, res){
    req.session.destroy();
    var msg = "Logged out";
    console.log(msg);
    res.end(msg);
});

app.listen(3000);

Do you know what may be wrong ?

node: v0.4.12 connect: v1.7.1

Thanks, - Nicolas

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号