Luis Arriojas
Luis Arriojas

Reputation: 571

Can't use connect-redis in Express.js

I'm studying Express.js and learned how to handle sessions using cookie-session module. I'm trying to use express-session but I have problems.

Previously, I was using cookie-session instead of express-session, and memoryStore worked perfectly. However, I couldn't use connect-redis with cookie-session, for that reason I installed express-session, however with it, I can't use any sort of store, nor memoryStore neither Redis.

The Redis DB is on redistogo and its URI looks like this: (masked for security although this is only to practice) redis://redistogo:e34d3***********************[email protected]:10072/

However, when I run "node app", it prints an error.

My package.json:

{
   "name": "application-name",
   "version": "0.0.1",
   "author": "Me <[email protected]>",
   "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"
   }
}

The "npm ls" show the following:

├─┬ [email protected]
│ ├── [email protected]
│ └── [email protected]
├─┬ [email protected]
│ ├── [email protected]
│ └── [email protected]
├─┬ [email protected]
│ ├─┬ [email protected]
│ │ ├── [email protected]
│ │ └── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
│ ├─┬ [email protected]
│ │ └─┬ [email protected]
│ │   ├── [email protected]
│ │   ├── [email protected]
│ │   └── [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
│ └── [email protected]
└─┬ [email protected]
  ├── [email protected]
  ├── [email protected]
  ├── [email protected]
  ├── [email protected]
  ├── [email protected]
  └── [email protected]

My 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***********************[email protected]: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!!');
});

The error showed when "node app" is ran:

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

What session module should I use? And how can I set up it with Redis?

Upvotes: 1

Views: 1638

Answers (2)

shodiqul muzaki
shodiqul muzaki

Reputation: 108

you need redis client

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

Upvotes: 0

mscdex
mscdex

Reputation: 106696

This:

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

should be:

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

EDIT: You also need to npm install cookie-parser and include var cookieParser = require('cookie-parser'); at the top and then app.use(cookieParser()); right before your app.use(session({ ... }));

Upvotes: 2

Related Questions