NodeJs - Using redis, connect-redis with express

2019-03-10 15:29发布

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

3条回答
孤傲高冷的网名
2楼-- · 2019-03-10 16:08

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.

查看更多
Juvenile、少年°
3楼-- · 2019-03-10 16:13

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.

查看更多
We Are One
4楼-- · 2019-03-10 16:15

In addition to what is mentioned, I would like to add express.session{..} throws following error as session is out of express core now.

Error('Most middleware (like ' + name + ') is no longer bundled with Express an....

Solution: add

var session = require('express-session');

And use plain session instead of express.session

查看更多
登录 后发表回答