小编典典

NodeJs-使用redis,connect-redis和express

redis

注意对于那些使用Redis挣扎的人,必须启动Redis服务器。
在Windows上,有一个redis发行版,请查看以下链接:https
:
//github.com/dmajkic/redis/downloads,然后通过启动“
redis-server.exe”来启动服务器。

我正在遵循有关node.js的教程。本教程使用Express和Redis。我安装了redis和connect-
redis(在package.json中引用了它们):

npm install redis connect-redis --save

在我的server.js中(仅是有意义的部分):

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'));
});

错误信息:

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开始监听3000端口,这是我所期望的。redis错误消息提到端口6379上的连接。如果我将redisClient传递给RedisStore,就会发生这种情况,这是我理解的绑定redis和RedisStore的工作。

我正在Windows上进行开发


阅读 656

收藏
2020-06-20

共1个答案

小编典典

看来您没有运行Redis服务器。您在redis.io/download上有一个很好的解释,说明如何下载,安装并运行服务器和客户端。

2020-06-20