title: How to add/remove response headers in ExpressJS while proxying request
body: <p>I have some files stored on a CDN server which is not to be directly accessed from client. So I proxy the requests via the public accessible server running ExpressJS and use <code>request</code> module to fetch the data server-side and return it in response.</p>
  
  <p>It is working and in code looks something like this:</p>
  
  <pre><code>var request = require('request');
  var app = express();
  var internalUrl = 'https://my.storage-cdn.com/private/info/file.xml';
  
  app.get('/somefile.xml', function (req, res) {
      request(internalUrl).pipe(res);
  });
  </code></pre>
  
  <p>The issues I faced with above method are:</p>
  
  <ul>
  <li>the storage/cdn server appends some response headers of its own
  which include some private information and as such can be a security
  issue when exposed in response. And above method of piping the <code>res</code>
  object to <code>request</code> doesn't remove those headers. It passes those
  headers as is to response. I want to remove those headers. </li>
  <li>I want to add some eTag and cache-control headers so the file could get cached
  properly.</li>
  </ul>
  
  <p>I have tried changing it to something like this:</p>
  
  <pre><code>app.get('/somefile.xml', function (req, res) {
      request(internalUrl, function (err, response, body) {
          if (!err &amp;&amp; response.statusCode == 200) {
              res.writeHead(200, {...});   // write custom headers I need
              res.end(body);
          }
      });
  });
  </code></pre>
  
  <p>This allows me to overwrite the headers to my liking, but in this method I have to wait for whole file to get downloaded on the server side first before I start sending the bytes in my response and with some files being as large as 1MB, it really affects the response time adversely.</p>
  
  <p>So my question is - is there a way to not have to wait for whole file to download on server side before start sending response but still be able to manipulate response headers?</p>