0

I have a node typescript project where I have created a TS file for the Redis connection which is below.

import { createClient } from 'redis';
import { promisify } from 'util';
import Logger from 'utils/logger';

const { REDIS_URL = 'redis://localhost:6379' } = process.env;
const options = {
    legacyMode: true,
    url: REDIS_URL,
}


const client = createClient(options);
// client.connect();

client.on('connect', () => { 
    Logger.info("Connected to Redis");
});
client.on('error', err => { 
    Logger.error('redis error: ' + err);
    init();
});

client.on('ready', err => { 
    Logger.info("redis is ready");
});

client.on('end', err => { 
    Logger.info("redis connection is ended");
});

//reconnecting
client.on('reconnecting', err => { 
    Logger.info("redis connection is reconnecting");
});


const init = async () => {
    await client.connect();
}

export { init,client };

then I am importing it and connected it to index.ts

import { init } from 'dataSource/redis';


(async () => {
  await init();
})();

app.listen(PORT,() => {
    // console.log(`server is running on PORT ${PORT}`)
    Logger.info(`Server Started in port : ${PORT}!`);
})

then I am trying to use the client in my controller file.

import {  client as redisClient } from 'datasource/redis';

redisClient.setEx("Key",Number(process.env.REDIS_EXPIRE_TIME),"VALUE");

but I am getting this error

Error: The client is closed
4
  • Does this answer your question? Redis NodeJs server error,client is closed Commented Mar 9, 2022 at 19:56
  • No I had tried that as well, it's not working. Commented Mar 9, 2022 at 21:27
  • I am using node-redis 4 Commented Mar 9, 2022 at 21:29
  • Having the same problem tbh Commented Mar 27, 2022 at 5:31

1 Answer 1

0

uncomment the "client.connect();" on line 13.

This should make it work

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.