NodeJs - 使用 redis,用 express 连接 redis

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12038128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:11:40  来源:igfitidea点击:

NodeJs - Using redis, connect-redis with express

node.jsexpressredis

提问by roland

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/downloadsthen start the server by launching "redis-server.exe"

注意对于那些在 Redis 上苦苦挣扎的人,必须启动 Redis 服务器。在 Windows 上,有一个 redis 发行版,请查看以下链接:https: //github.com/dmajkic/redis/downloads然后通过启动“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):

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

npm install redis connect-redis --save

In my server.js (only meaningful part):

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

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.

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

I am developing on Windows

我正在 Windows 上开发

采纳答案by Mr. Goferito

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

看起来您没有运行 redis 服务器。您在redis.io/download 上有关于如何下载、安装和运行服务器和客户端的很好的解释。

回答by Dwight Spencer

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.

例如,当您设置与 Redis 存储的连接时,您会告诉应用程序 Redis 服务器位于何处以及它正在侦听的端口。还可以同时删除 port 指令,connect-redis 将使用远程 redis 服务器的默认端口。

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 telnetcan 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.

我确实忘记说明命令netstat,pingtelnet可以帮助调试本地打开哪些端口以及服务返回给应用程序的内容。如果您在 Linux、OSX 或 BSD 等 unix 环境中,这两个命令将在 cmd.exe/powershell 和 bash 下执行。

An example of this would be exectuting the following:

这方面的一个例子是执行以下内容:

Windows:

视窗:

netstat -np tcp | find "3000"

netstat -np tcp | find "6379"

Linux:

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.

它的作用是报告 localhost:3000 或 localhost:6379 的本地打开端口。如果您使用远程系统,那么您将使用 ping 来查看服务器是否已启动,并使用像 nmap 这样的端口扫描程序来发现可用的远程端口。

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.

请记住,仅仅因为使用 Web 语言进行编程并不意味着您没有学习网络的技术目的。

回答by zion inc

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

除了提到的内容之外,我想添加 express.session{..}以下错误,因为会话现在不在快速核心之外。

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

Solution: add

解决方法:添加

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

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

And use plain sessioninstead of express.session

并使用普通session而不是express.session