3

A sample of my code will be:

middlewareA(req,res,next){
  res.send('some msg to client');
  next()
}
middlewareB(req,res,next){
  var sent_msg_body = res.<some method\property to get body>
  logger.info(sent_msg_body);     
}

I woukd like to retrieve the msg that was sent using res.send(msg);

Is there an option in Express.js's res object to do that after the response was already sent?

yuria
  • 541
  • 2
  • 7
  • 22
  • can you be more specific in this question? – abdulbarik Sep 21 '16 at 07:16
  • Why you want to get sent information from `res` object while you can get it during res.send() ? – abdulbarik Sep 21 '16 at 07:31
  • it is a middleware, and I want to get the msg in the next middleware, that comes after the one that sent the msg. – yuria Sep 21 '16 at 07:33
  • Can you post your code here? so that is's more clear to resolve it. – abdulbarik Sep 21 '16 at 07:34
  • It seems to depend on the version of Express you're using. This existing question's answers shows how to modify a response in middleware in multiple version (in multiple answers): http://stackoverflow.com/questions/9896628/connect-or-express-middleware-to-modify-the-response-body - the same logic should allow you to only fetch it. – Gimby Sep 21 '16 at 08:16
  • ok so ive done it but i am having some weired behaviour. can you plz take a look? http://stackoverflow.com/questions/39611395/express-js-add-response-body – yuria Sep 21 '16 at 09:05

1 Answers1

2

You can use request object to get it

Do like this:

middlewareA(req,res,next){
  res.send('some msg to client');
  req.message='some msg to client';
  next();
}
middlewareB(req,res,next){
  var sent_msg_body = req.message
  logger.info(sent_msg_body);     
}
abdulbarik
  • 6,101
  • 5
  • 38
  • 59