I have built an api and i would like to log all of the requests and responses made.
I have a route GET: api/transactions
In this route i return the following response. This wrapper response is quite common throughout my application.
return res.status(200).json({
success: true,
status: 200,
data: transaction,
message: "Successfully retrieved transaction",
});
I have a middleware that is dealing with creating log entries. Inside the res.on("finish")
event i want to access the json data i am returning above.
app.use(responseTime((req, res, time) => {
res.on("finish", () => {
// console.log(res);
const responseLog = {
log_id: logId,
line_data_statusCode: res.statusCode,
line_data_statusMessage: res.statusMessage,
line_data_responseTime: time,
message: ".", // get res json message
};
this.log.info({ ...responseLog, ...{ log_type: "response" } });
});
}));
How can i access the res data from the event.