Home > Enterprise >  Redis server is running but no response from the server
Redis server is running but no response from the server

Time:12-22

I am running a redis server for my project and it seems to work fine. I checked it by running the command

redis-cli

But when I try running the following code it doesn't give me any response

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

client.on("error", function(err){
    console.error("Errro encountered: ", err);
});

client.on("connect", () => {
    console.log("Redis connected");
});

Can someone please help me. I am totally new into this.

CodePudding user response:

In your code you are creating a client, you are properly installing event handlers but you forgot to actually connect to the redis server.

Using this code:

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

client.on("error", function(err){
    console.error("Errro encountered: ", err);
});

client.on("connect", () => {
    console.log("Redis connected");
});

client.connect(); // connect to the server here!

You will get the result that you probably expected. Basic example in the github readme of the redis node module should be a good reference for that.

EDIT: Apparently, the module used to automatically connect to a database during the createClient() call, but it does not anymore due to some breaking changes between versions, so now an explicit client.connect() call is needed.

  • Related