-1

I have a simple backend application built with Node.js. The directory structure as below enter image description here

and this is GitHub Repository Link here below is the code of server.js file

// server.js
const express = require('express');
const bodyParser = require('body-parser');
const dotenv = require('dotenv');
dotenv.config();

const app = express();
const port = process.env.PORT || 3000;

app.use(bodyParser.json());

try {
    // Import routes
    const adminRoutes = require('./routes/adminRoutes');

    // Use routes
    app.use('/api/admins', adminRoutes);  // Correct base URL for admin routes
   
} catch (error) {
    console.log(error);
}

app.listen(port, () => {
    console.log(`Server running on port ${port}`);
});

and redisClient.js file

const redis = require('redis');
const dotenv = require('dotenv');
dotenv.config();

var redisClient;

const initializeRedisClient = () => {
    if (!redisClient) {
        redisClient = redis.createClient();
        redisClient.on('error', (err) => console.log('Redis Client Error:', err));
        redisClient.on('connect', () => console.log('Connected to Redis'));
        redisClient.on('end', () => console.log('Redis client disconnected'));
        redisClient.on('reconnecting', () => console.log('Redis client reconnecting'));
    }
    return redisClient;
};

module.exports = initializeRedisClient;

The issue comes when I try to request API which are using middleware of checkBlacklist.

Below is the issue

C:\workshop\Node JS\ecommerce-api\node_modules@redis\client\dist\lib\client\index.js:510 return Promise.reject(new errors_1.ClientClosedError()); ^

ClientClosedError: The client is closed at Commander._RedisClient_sendCommand (C:\workshop\Node JS\ecommerce-api\node_modules@redis\client\dist\lib\client\index.js:510:31) at Commander.commandsExecutor (C:\workshop\Node JS\ecommerce-api\node_modules@redis\client\dist\lib\client\index.js:190:154) at BaseClass. [as get] (C:\workshop\Node JS\ecommerce-api\node_modules@redis\client\dist\lib\commander.js:8:29) at checkBlacklist (C:\workshop\Node JS\ecommerce-api\routes\adminRoutes.js:14:21) at Layer.handle [as handle_request] (C:\workshop\Node JS\ecommerce-api\node_modules\express\lib\router\layer.js:95:5) at next (C:\workshop\Node JS\ecommerce-api\node_modules\express\lib\router\route.js:149:13) at C:\workshop\Node JS\ecommerce-api\middlewares\authMiddleware.js:13:17 at C:\workshop\Node JS\ecommerce-api\node_modules\jsonwebtoken\verify.js:261:12 at getSecret (C:\workshop\Node JS\ecommerce-api\node_modules\jsonwebtoken\verify.js:97:14) at module.exports [as verify] (C:\workshop\Node JS\ecommerce-api\node_modules\jsonwebtoken\verify.js:101:10)

It looks like redis client connection is getting lost. Can someone please let me know what should I change in the code?

1 Answer 1

0

My guess is that you are working from old sample code or an old tutorial. Node Redis 4.x was release a couple of years ago and now supports Promises.

This question comes up a lot here on StackOverflow so I'm just going to point you to the Node Redis documentation which shows you how to create and open a client.

Not the answer you're looking for? Browse other questions tagged or ask your own question.