How to create a custom function for response in Express Js. I'm new to Node/express js, In Flask(Python) I can create a function (for eg: custom_response()) and return it as respone object. But when it comes to Express what I learnt is that I cannot create custom response object instead I can add properties to response objects. I'll explain it below:
const responseJSON = (res) => {
return (status, message, data) => {
const response = res.json({
status: status,
message: message,
data: data,
});
return response;
};
};
and I have created a middleware to pass attach the above function to response below:
const responseMiddleWare = (req, res, next) => {
res.responseJSON = responseJSON(res);
next();
};
app.use(responseMiddleWare);
so in route handler I can do like below:
router.get("/get-all-category",getAllCategory);
const getAllCategory = async (req, res) => {
return res.responseJSON(200, "Category lists")
so here Im using res.responseJSON(), instead I'd like to know how can I return responseJSON() instead by making responseJSON() as the response object.
Is it possible in Node/Express Js?