koque
koque

Reputation: 2256

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)

Upvotes: 0

Views: 918

Answers (1)

MrfksIV
MrfksIV

Reputation: 930

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

Upvotes: 1

Related Questions