node.jsredis-cache

How to configure redis client in node js


I tried install redis client in system and run node js code its working fine. but I tried to redis client configure to set host and port in node js.but its not connected its throwing connection refused error.how to resolve it any one give suggestion.

cache.js

//Import redis
const redis = require('redis');

const { promisify } = require('util');

const host = "127.0.0.1"

const port = 6379

//Connecting redis client
const client = redis.createClient(port,host,redis)

client.on('connect', function () {
    console.log("Redis Connected")
});

client.on('error', function (err) {
    console.log(err)
});

{ Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379 at Object._errnoException (util.js:1022:11) at _exceptionWithHostPort (util.js:1044:20) at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1182:14) code: 'ECONNREFUSED', errno: 'ECONNREFUSED', syscall: 'connect', address: '127.0.0.1', port: 6379 }


Solution

  • The code for connecting to redis seems to work fine. You can check if redis is running on your system:

    sudo service redis status
    

    If the status is not active try to restart the service. You can refer to this code for creating a better wrapper for redis: https://github.com/kumaranshu72/node_ts_boilerplate/blob/github/src/utils/redis.ts