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
Uday Mishra
  • 212
  • 2
  • 18

1 Answers1

0

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

This should make it work