gpt4 book ai didi

session 突然不起作用

转载 作者:IT王子 更新时间:2023-10-29 06:03:47 25 4
gpt4 key购买 nike

我对 session 有很大的疑问。

我致力于围绕“Redis”nodejs 创建一个应用程序,使用不同的库作为 express。

但是突然之间 session 不再有效。我不明白为什么。所以我重新格式化了我的服务器并正确地重新安装。(我使用的是 Debian 6)

而且基本测试仍然不起作用:(.

这是测试:

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

var RedisStore = require('connect-redis')(express);
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({ secret: "keyboard cat", store: new RedisStore }));

app.get('/', function(req, res){
var sess = req.session;
req.session.visitCount = req.session.visitCount ? req.session.visitCount + 1 : 1;

if (sess.views) {
res.setHeader('Content-Type', 'text/html');
res.write('<p>views: ' + sess.views + '</p>');
res.write('<p>expires in: ' + (sess.cookie.maxAge / 1000) + 's<p/>');
res.write('<p>You have visited this page ' + req.session.visitCount + ' times</p>');
res.end();
sess.views++;
} else {
sess.views = 1;
res.end('welcome to the session demo. refresh!');
}
});

app.listen(4000);

程序运行正确(没有错误)。但是当我使用“sess.views”时永远无效

所以我总是看到:欢迎来到 session 演示。刷新!

我有点绝望。

预先感谢您的帮助

最佳答案

代码

我相信 sess.views++ 需要在 res.end() 之前调用,就像 TJ's example

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

var RedisStore = require('connect-redis')(express);
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({ secret: "keyboard cat", store: new RedisStore }));

app.get('/', function(req, res){
var sess = req.session;
req.session.visitCount = req.session.visitCount ? req.session.visitCount + 1 : 1;

if (sess.views) {
res.setHeader('Content-Type', 'text/html');
res.write('<p>views: ' + sess.views + '</p>');
res.write('<p>expires in: ' + (sess.cookie.maxAge / 1000) + 's<p/>');
res.write('<p>You have visited this page ' + req.session.visitCount + ' times</p>');
sess.views++;
res.end();
} else {
sess.views = 1;
res.end('welcome to the session demo. refresh!');
}
});

app.listen(4000, '127.0.0.1');

安装包

alfred@alfred-laptop:~/database/redis-2.2.0-rc4/src$ npm ls
/home/alfred
├── connect-redis@1.0.6
├─┬ everyauth@0.2.15
│ ├─┬ connect@1.5.2
│ │ ├── connect-redis@1.0.6 extraneous
│ │ ├── mime@1.2.2
│ │ └── qs@0.2.0
│ ├── oauth@0.9.2
│ ├── openid@0.1.8
│ ├── restler@0.2.1
│ └─┬ xml2js@0.1.9
│ └── sax@0.1.4
├─┬ express@2.4.1
│ ├─┬ connect@1.5.2
│ │ ├── connect-redis@1.0.6 extraneous
│ │ ├── mime@1.2.2
│ │ └── qs@0.2.0
│ ├── mime@1.2.2
│ └── qs@0.2.0
├── hiredis@0.1.12
├── jade@0.12.4
├── node-expat@1.3.2
├── node-stringprep@0.0.5
├─┬ node-xmpp@0.2.9
│ └── ltx@0.0.5
├── nodeunit@0.5.1
├── notifo@0.0.2
├── openid@0.2.0
├─┬ optimist@0.2.5
│ └── wordwrap@0.0.1
├── recaptcha@1.1.0
├── redis@0.6.6
└─┬ socket.io@0.7.6
├── policyfile@0.0.3
├── redis@0.6.0
└── socket.io-client@0.7.3

系统信息

alfred@alfred-laptop:~/database/redis-2.2.0-rc4/src$ npm -v
1.0.15

alfred@alfred-laptop:~/database/redis-2.2.0-rc4/src$ node -v
v0.4.9

alfred@alfred-laptop:~/database/redis-2.2.0-rc4/src$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=10.10
DISTRIB_CODENAME=maverick
DISTRIB_DESCRIPTION="Ubuntu 10.10"

curl 示例

alfred@alfred-laptop:~/node/stackoverflow/6700472$ curl -c cookie http://localhost:4000/
welcome to the session demo. refresh!alfred@alfred-laptop:~/node/stackoverflow/6700472$ curl -b cookie http://localhost:4000/
<p>views: 1</p><p>expires in: 14396.223s<p/><p>You have visited this page 2 times</p>alfred@alfred-laptop:~/node/stackoverflow/6700472$ curl -b cookie http://localhost:4000/
<p>views: 2</p><p>expires in: 14399.221s<p/><p>You have visited this page 3 times</p>alfred@alfred-laptop:~/node/stackoverflow/6700472$

工作得很好!

关于 session 突然不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6700472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com