会话突然不起作用

时间:2011-07-14 22:03:30

标签: session node.js redis express

我的会话存在很大问题。

我开始围绕“Redis”nodejs使用不同的库创建一个应用程序。

但突然间会议不再有效。我不明白为什么。 所以我重新格式化了我的服务器并正确地重新安装。 (我正在使用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”时永远无效

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

我有点绝望。

提前感谢您的帮助

1 个答案:

答案 0 :(得分:5)

我认为需要在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"

卷曲示例

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$ 

工作得很好!