This is my server call to route
const redis = require("redis");
const client = redis.createClient();
client.connect();
module.exports = client;
This is my API route
app.get("/api/ping", middleWare.cacheRouteOne, RouteController.routeOne);
This is my cache route function
exports.cacheRouteOne = (req, res, next) => {
client.get("ping", (err, data) => {
console.log(data);//To check whether is working or not.
if (err) throw err;
if (data != null) {
res.json({
postss: JSON.parse(data),
});
} else {
next();
}
});
};
And this is my API call code
exports.routeOne = (req, res) => {
axios
.get("some APi CALL")
.then((response) => {
client.setEx("ping", 3600, JSON.stringify(response.data.posts));
console.log(client);
res.status(200).json({
status: true,
posts: response.data.posts,
});
});
};
When it calls the middle function it hangs my code after clinet.get()
2
Answers
I got the answer, I was making a call back but for forget to add await , so this is the updated middleware code
use this code to connect redis