简体   繁体   English

如何在node.js中使用Redis?

[英]How do I use redis in node.js?

I am trying to use redis using this segment of code: 我正在尝试使用以下代码段使用redis:

const redis = require('redis');
const client = redis.createClient();

I am getting the following error: 我收到以下错误:

events.js:174
  throw er; // Unhandled 'error' event
  ^

Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1097:14)

You will need to first install redis on your system. 您将需要先在系统上安装redis。 To do so, follow the instructions here . 为此,请按照此处的说明进行操作。

Then run the following command in your command prompt/terminal: 然后在命令提示符/终端中运行以下命令:

redis-server

If you want to run redis in the background without having to start it everytime, then you can use: 如果您想在后台运行redis而不必每次都启动它,则可以使用:

redis-server --daemonize yes

You can check if your server is running by using the command: 您可以使用以下命令检查服务器是否正在运行:

redis-cli set a 3 & redis-cli get a redis-cli set a 3redis-cli get a

If this works then your redis server has started successfully. 如果这可行,则您的Redis服务器已成功启动。 After this, your previous code should be able to connect to the redis-server provided the server is running on the default port (6379). 此后,只要服务器在默认端口(6379)上运行,您以前的代码就应该能够连接到redis服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM