0

in server.js file i have added the below code from ref. it gives me req.body as expected. but is it possible to log the response.body as well? how. i tried to print the res.body it gives me long object of response but not actual response body.

app.middleware('initial', function logResponse(req, res, next) {
  // install a listener for when the response is finished
  res.on('finish', function() {
    // the request was handled, print the log entry

    console.log(res.body, req.originalUrl, res.statusCode);
  });

  // resume the routing pipeline,
  // let other middleware to actually handle the request
  next();
});
Farid Nouri Neshat
  • 29,438
  • 6
  • 74
  • 115
Bhavin Rana
  • 1,554
  • 4
  • 20
  • 40

1 Answers1

0

A bit late: Although Loopback uses express this is a loopback specific question, so not a duplicate. You need to access the current context not the response obj, the context will have the response obj. A workaround is logging using a hook "afterRemote(**..." or enable enableHttpContext for middleware in config.json

"remoting": {
    "context": {
      "enableHttpContext": true
    }
Ed Barahona
  • 721
  • 5
  • 7