express ajax session,javascript - Node.js & Express session problem - Stack Overflow

博主在Node.js应用中遇到会话管理的问题,具体表现为在从'/twitter/connect/'跳转到'/twitter/callback/'的过程中,session变量有时在下一页请求时变为undefined。已确认在设置session变量时值是正确的,但它们在页面间请求中无法保持。问题可能出现在session存储和恢复的环节,尤其是在首次请求时。博主使用了express session中间件,并提供了应用配置代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I'm having a problem with sessions, where sometimes the session variable I just set

is undefined on the next page request. I typically have to go through the flow

again in order to properly set the variables.

I can confirm that I'm not trying to set the session variables to undefined; they have a legit value.

In my app, users move from /twitter/connect/ to /twitter/callback/. The former retreives some oauth data from twitter, the latter logs the user into twitter.

/twitter/connect/ is simple:

app.get('/twitter/connect/?', function(req, res){

consumer().getOAuthRequestToken(function(error, oauthToken, oauthTokenSecret, results){

if (error){

// error handling here

} else {

req.session.oauthRequestToken = oauthToken;

req.session.oauthRequestTokenSecret = oauthTokenSecret;

// if I console.log the two session variables above

// they have the proper values.

res.redirect("https://twitter.com/oauth/authorize?oauth_token="+req.session.oauthRequestToken);

}

});

});

After that, twitter sends them back to /twitter/callback/:

app.get('/twitter/callback/?', function(req, res){

console.log(req.session.oauthRequestToken);

console.log(req.session.oauthRequestTokenSecret);

// more often than not, the two variables above are

// undefined. but not always. usually on the first

// pass, never on the second.

});

I have no idea what's going on, I can confirm that the session variables are being set properly, they just aren't holding their value in between page requests, but only the first time.

This is how I'm creating my server:

app.configure('development', function(){

app.use(express.cookieParser());

app.use(express.session({ secret:'yodawgyo' }));

app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));

app.use(express.logger());

app.use(express.static(__dirname + '/public'));

app.set('view engine', 'ejs');

app.set('view options', {

open: '{{',

close: '}}'

});

});

I just have a dev environment for now. I have Node 0.5.0-pre installed, but saw this issue on 0.4.1 as well. I'm using express 2.3.2.

Any help is much appreciated.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值