简体   繁体   中英

How do I use redis in node.js?

I am trying to use redis using this segment of code:

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. 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-server --daemonize yes

You can check if your server is running by using the command:

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

If this works then your redis server has started successfully. After this, your previous code should be able to connect to the redis-server provided the server is running on the default port (6379).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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