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

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

问题描述

注意
对于那些与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:\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的情况。

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,connect-redis与express的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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