NodeJs - Using redis, connect-redis with express


NOTE For those struggling with Redis, the Redis server has to be launched. On windows, there is a redis distribution, check out the following link: then start the server by launching "redis-server.exe"

I am following along a tutorial on node.js. The tutorial uses Express and Redis. I installed redis and connect-redis (they are referenced in package.json):

npm install redis connect-redis --save

In my server.js (only meaningful part):

var express = require('express');
var http = require('http');
var app = module.exports = express();
var RedisStore = require('connect-redis')(express);

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

  app.set('port', process.env.PORT || 3000);
  app.set('views', __dirname + '/views');
  console.log('views', __dirname + '/views');
  app.set('view engine', 'jade'); //jade as template engine
      secret: "kqsdjfmlksdhfhzirzeoibrzecrbzuzefcuercazeafxzeokwdfzeijfxcerig",
      store: new RedisStore({ host: 'localhost', port: 3000, client: redis })
  app.use(express.static(__dirname + '/public'));

The error message:

Express server listening on port 3000
[ERROR] Error
Error: Redis connection to failed - connect ECONNREFUSED
    at RedisClient.on_error (D:\Programming\Screencasts\peepcode\nodejs\peepcode
    at Socket.<anonymous> (D:\Programming\Screencasts\peepcode\nodejs\peepcode-0

    at Socket.EventEmitter.emit (events.js:88:17)
    at Socket._destroy.self.errorEmitted (net.js:329:14)
    at process.startup.processNextTick.process._tickCallback (node.js:244:9)
[ERROR] Error
Error: Redis connection to failed - connect ECONNREFUSED
    at RedisClient.on_error (D:\Programming\Screencasts\peepcode\nodejs\peepcode
    at Socket.<anonymous> (D:\Programming\Screencasts\peepcode\nodejs\peepcode-0

Express starts listening on port 3000, which is what I expect. The redis error message mentions connection on port 6379. This happens if I pass the redisClient to RedisStore, which is what I understood to do to bind redis and RedisStore.

I am developing on Windows

8/22/2012 7:25:36 AM

Accepted Answer

It looks like you don't have the redis server running. You have a good explanation on about how to download, install it and run both server and client.

8/20/2012 5:29:25 PM

The code provide is fine, just configured wrong. All that needs to be changed is the port number.

For example when one goes to setup the connection to a redis storage one is telling the application where the Redis server is located and at what port it is listening on. One could also drop the port directive all together and connect-redis will use the default port for the remote redis server.

In this case I would suggest to try this code snippet:


store: new RedisStore({ ..., port: 3000, ... })


store: new RedisStore({..., port: 6379, ... })


I did forget to state that the commands netstat, ping, and telnet can help one to debug which ports are open locally and what the service is returning to the application. These two commands would be executed in cmd.exe/powershell and under bash if your in a unix environment such as Linux, OSX, or BSD.

An example of this would be exectuting the following:


netstat -np tcp | find "3000"

netstat -np tcp | find "6379"


netstat -nlt | grep '3000\|6379'

What this does is reports the locally opened ports for either localhost:3000 or localhost:6379. If your working with a remote system then you would use ping to see if the server is up and a portscanner like nmap to discover the remote ports available.

Following all this you would then initiate the connection by using:

telnet <host> 3000
telnet <host> 6379

Remember, just because one is programming in a web language that doesn't mean one is not learning the technical ends of networking either.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow