Skip to content

Instantly share code, notes, and snippets.

@mid0111
Created May 17, 2016 03:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mid0111/80d34e2123bfee1fae0e9cd3979a3c84 to your computer and use it in GitHub Desktop.
Save mid0111/80d34e2123bfee1fae0e9cd3979a3c84 to your computer and use it in GitHub Desktop.
express dump response body

http://stackoverflow.com/questions/19215042/express-logging-response-body

Not sure if it's the simplest solution, but you can write a middleware to intercept data written to the response. Make sure you disable app.compress().

function logResponseBody(req, res, next) {
  var oldWrite = res.write,
      oldEnd = res.end;

  var chunks = [];

  res.write = function (chunk) {
    chunks.push(chunk);

    oldWrite.apply(res, arguments);
  };

  res.end = function (chunk) {
    if (chunk)
      chunks.push(chunk);

    var body = Buffer.concat(chunks).toString('utf8');
    console.log(req.path, body);

    oldEnd.apply(res, arguments);
  };

  next();
}

app.use(logResponseBody);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment