Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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: https://github.com/dmajkic/redis/downloads 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.configure(function(){
  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
  app.use(express.favicon());
  app.use(express.logger('dev'));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.cookieParser());
  app.use(express.session({
      secret: "kqsdjfmlksdhfhzirzeoibrzecrbzuzefcuercazeafxzeokwdfzeijfxcerig",
      store: new RedisStore({ host: 'localhost', port: 3000, client: redis })
  }));
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
});

The error message:

Express server listening on port 3000
[ERROR] Error
Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED
    at RedisClient.on_error (D:\Programming\Screencasts\peepcode\nodejs\peepcode
-069-full-stack-nodejs-i-mov\code\roland\HotPie\node_modules\redis\index.js:140:
24)
    at Socket.<anonymous> (D:\Programming\Screencasts\peepcode\nodejs\peepcode-0
69-full-stack-nodejs-i-mov\code\roland\HotPie\node_modules\redis\index.js:74:14)

    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 127.0.0.1:6379 failed - connect ECONNREFUSED
    at RedisClient.on_error (D:\Programming\Screencasts\peepcode\nodejs\peepcode
-069-full-stack-nodejs-i-mov\code\roland\HotPie\node_modules\redis\index.js:140:
24)
    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

like image 513
roland Avatar asked Aug 20 '12 12:08

roland


People also ask

How do I connect Redis to Express?

Express session with Redis To add support of Redis you have to use Redis client and connect-redis. Create express-session and pass it to connect-redis object as parameter. This will initialize it. Then in session middleware, pass the Redis store information such as host, port, and other required parameters.

How does Redis integrate with node js?

Create new session. js file in the root directory with the following content: const express = require('express'); const session = require('express-session'); const redis = require('redis'); const client = redis. createClient(); const redisStore = require('connect-redis')(session); const app = express(); app.


2 Answers

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:

Change:

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

New:

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

UPDATE:

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:

Windows:

netstat -np tcp | find "3000"

netstat -np tcp | find "6379"

Linux:

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.

like image 191
Dwight Spencer Avatar answered Oct 08 '22 16:10

Dwight Spencer


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

like image 22
Mr. Goferito Avatar answered Oct 08 '22 14:10

Mr. Goferito