title: How to get the size of a Node.js writable stream (HTTP server response)
body: <p>I would like to write an express middleware printing HTTP access log in the Common (Access) Log format. The last column in this format is the size of the response body in bytes.</p>
  
  <p>How to determine a size of the HTTP response produced by the Node.js server?</p>
  
  <p>Example that does not work:</p>
  
  <pre><code>app.use(function(req, res, next) {
    var bytes = 0;
    res.on('data', function(chunk) {
      bytes += chunk.length;
    });
    res.on('finish', function() {
      // `bytes` is the response size
      console.log(/* common log entry */);
    });
  });
  </code></pre>
  
  <p>I have done some research, for example Hapi's <a href="https://github.com/rockbot/hapi-common-log/blob/bc5c2d03737ad6fb690a4735569f850e410e070d/index.js#L17" rel="noreferrer">common-log</a> always emits <code>-</code> as the size. Does it mean it is not possible to obtain the size?</p>