无法在Express.js中使用connect-redis

时间:2014-04-23 15:55:07

标签: node.js session express redis cookie-session

我正在研究Express.js并学习如何使用cookie-session模块处理会话。我正在尝试使用快速会话,但我遇到了问题。

以前,我使用的是cookie-session而不是express-session,而且memoryStore工作得很好。但是,我无法将connect-redis与cookie-session一起使用,因此我安装了快速会话,但是使用它,我不能使用任何类型的存储,也不能使用memoryStore和Redis。

Redis DB在redistogo上,它的URI如下所示:(为安全而屏蔽,虽然这只是练习) redis的:// redistogo:e34d3***********************f4bb@albacore.redistogo.com:10072 /

但是,当我运行“node app”时,它会输出错误。

我的package.json:

{
   "name": "application-name",
   "version": "0.0.1",
   "author": "Me <me@gmail.com>",
   "license": "MIT",
   "private": true,
   "scripts": {
      "start": "node app"
   },
   "dependencies": {
     "express": "4.x.x",
     "cookie-parser": "1.x.x",
     "express-session": "1.x.x",
     "connect-redis": "2.x.x"
   }
}

“npm ls”显示以下内容:

├─┬ connect-redis@2.0.0
│ ├── debug@0.8.0
│ └── redis@0.10.1
├─┬ cookie-parser@1.0.1
│ ├── cookie@0.1.0
│ └── cookie-signature@1.0.3
├─┬ express@4.0.0
│ ├─┬ accepts@1.0.0
│ │ ├── mime@1.2.11
│ │ └── negotiator@0.3.0
│ ├── buffer-crc32@0.2.1
│ ├── cookie@0.1.0
│ ├── cookie-signature@1.0.3
│ ├── debug@0.8.0
│ ├── escape-html@1.0.1
│ ├── fresh@0.2.2
│ ├── merge-descriptors@0.0.2
│ ├── methods@0.1.0
│ ├── parseurl@1.0.1
│ ├── path-to-regexp@0.1.2
│ ├── qs@0.6.6
│ ├── range-parser@1.0.0
│ ├─┬ send@0.2.0
│ │ └── mime@1.2.11
│ ├─┬ serve-static@1.0.1
│ │ └─┬ send@0.1.4
│ │   ├── fresh@0.2.0
│ │   ├── mime@1.2.11
│ │   └── range-parser@0.0.4
│ ├─┬ type-is@1.0.0
│ │ └── mime@1.2.11
│ └── utils-merge@1.0.0
└─┬ express-session@1.0.3
  ├── buffer-crc32@0.2.1
  ├── cookie@0.1.2
  ├── cookie-signature@1.0.3
  ├── debug@0.7.4
  ├── uid2@0.0.3
  └── utils-merge@1.0.0

我的app.js:

//create express app
var http = require('http');
var express = require('express');
var app = express();

//add express-session
var session = require('express-session');
var RedisStore = require('connect-redis')(express);
app.use(session({
   store: new RedisStore({
      host: "redis://redistogo:e34d3***********************f4bb@albacore.redistogo.com:10072/"
   })
}));

//routing
var router1 = require('./router/router1')(app);
var router2 = require('./router/router2')(app);

//Deploying server
http.createServer(app).listen(3000, function () {
   console.log('Deployed!!');
});

“节点应用”运行时显示错误:

luis@luis-laptop ~/www/express2 $ node app

/home/luis/www/express2/node_modules/connect-redis/lib/connect-redis.js:96
  RedisStore.prototype.__proto__ = Store.prototype;
                                    ^
TypeError: Cannot read property 'prototype' of undefined
    at module.exports (/home/luis/www/express2/node_modules/connect-redis/lib/connect-redis.js:96:41)
    at Object.<anonymous> (/home/luis/www/express2/app.js:18:42)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:902:3

我应该使用哪个会话模块?我如何使用Redis进行设置?

2 个答案:

答案 0 :(得分:2)

此:

var RedisStore = require('connect-redis')(express);

应该是:

var RedisStore = require('connect-redis')(session);

编辑:您还需要npm install cookie-parser并在var cookieParser = require('cookie-parser');

之前的app.use(cookieParser());位于顶部,然后app.use(session({ ... }));包含{{1}}

答案 1 :(得分:0)

你需要redis客户端

var redis = require('redis').createClient();