Nodejs - Express res.download giving Can't set headers after they are sent exception
<p>I want to make an api that will serve files of any extensions.
Like this: <a href="http://localhost/download/[file].[extension]" rel="nofollow">http://localhost/download/[file].[extension]</a></p>

<p>Here is my code, but it is intermittently giving this message: Can't set headers after they are sent.</p>

<pre><code>  var express = require('express');
  var app = express();

  app.get('/download/:fileName/:extension', function(req, res){
    var file = __dirname + '/' + req.params.fileName + '.' + req.params.extension;
    res.download(file, function(err){
        if (err) {
          res.sendStatus(404);
        }
      res.end();
    }); 
  });

  var server = app.listen(3000, function () {
    var host = server.address().address;
    var port = server.address().port;

    console.log('app listening at http://%s:%s', host, port);
  });
</code></pre>