NodeJs - 使用 redis,用 express 连接 redis [英] NodeJs - Using redis, connect-redis with express

查看:211
本文介绍了NodeJs - 使用 redis,用 express 连接 redis的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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"

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

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

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

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

错误信息:

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:ProgrammingScreencastspeepcode
odejspeepcode
-069-full-stack-nodejs-i-movcode
olandHotPie
ode_modules
edisindex.js:140:
24)
    at Socket.<anonymous> (D:ProgrammingScreencastspeepcode
odejspeepcode-0
69-full-stack-nodejs-i-movcode
olandHotPie
ode_modules
edisindex.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:ProgrammingScreencastspeepcode
odejspeepcode
-069-full-stack-nodejs-i-movcode
olandHotPie
ode_modules
edisindex.js:140:
24)
    at Socket.<anonymous> (D:ProgrammingScreencastspeepcode
odejspeepcode-0

Express 开始侦听端口 3000,这正是我所期望的.redis 错误消息提到了端口 6379 上的连接.如果我将 redisClient 传递给 RedisStore,就会发生这种情况,这就是我理解的绑定 redis 和 RedisStore.

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.

我正在 Windows 上开发

I am developing on Windows

推荐答案

您似乎没有运行 redis 服务器.您对 redis.io/download 有很好的解释,了解如何下载、安装它并运行服务器和客户端.

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.

这篇关于NodeJs - 使用 redis,用 express 连接 redis的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆