Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3663

Redis client initialization Issue in Node Js application

$
0
0

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

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

// server.jsconst 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\NodeJS\ecommerce-api\node_modules@redis\client\dist\lib\client\index.js:510return Promise.reject(new errors_1.ClientClosedError());^

ClientClosedError: The client is closedat 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:17at C:\workshop\Node JS\ecommerce-api\node_modules\jsonwebtoken\verify.js:261:12at 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?


Viewing all articles
Browse latest Browse all 3663

Trending Articles