title: How to use filesystem's createReadStream with Meteor router(NodeJS)
body: <p>I need to allow the user of my app to download a file with Meteor. Currently what I do is when the user requests to download a file I enter into a "fileRequests" collection in Mongo a document with the file location and a timestamp of the request and return the ID of the newly created request. When the client gets the new ID it imediately goes to mydomain.com/uploads/:id. I then use something like this to intercept the request before Meteor does:</p>
  
  <pre><code>var connect = Npm.require("connect");
  var Fiber = Npm.require("fibers");
  var path = Npm.require('path');
  var fs = Npm.require("fs");
  var mime = Npm.require("mime");
  
  __meteor_bootstrap__.app
      .use(connect.query())
      .use(connect.bodyParser()) //I add this for file-uploading
      .use(function (req, res, next) {
          Fiber(function() {
  
              if(req.method == "GET") {
                  // get the id here, and stream the file using fs.createReadStream();
              }
              next();
          }).run();
      });
  </code></pre>
  
  <p>I check to make sure the file request was made less than 5 seconds ago, and I immediately delete the request document after I've queried it.</p>
  
  <p>This works, and is secure(enough) I think. No one can make a request without being logged in and 5 seconds is a pretty small window for someone to be able to highjack the created request URL but I just don't feel right with my solution. It feels dirty!</p>
  
  <p>So I attempted to use <a href="https://github.com/tmeasday/meteor-router" rel="noreferrer">Meteor-Router</a> to accomplish the same thing. That way I can check if they're logged in correctly without doing the 5 second open to the world trickery.</p>
  
  <p>So here's the code I wrote for that:</p>
  
  <pre><code>    Meteor.Router.add('/uploads/:id', function(id) {
  
      var path = Npm.require('path');
      var fs = Npm.require("fs");
      var mime = Npm.require("mime");
  
      var res = this.response;
  
      var file = FileSystem.findOne({ _id: id });
  
      if(typeof file !== "undefined") {
          var filename = path.basename(file.filePath);
          var filePath = '/var/MeteorDMS/uploads/' + filename;
  
          var stat = fs.statSync(filePath);
  
          res.setHeader('Content-Disposition', 'attachment; filename=' + filename);
          res.setHeader('Content-Type', mime.lookup(filePath));
          res.setHeader('Content-Length', stat.size);
  
          var filestream = fs.createReadStream(filePath);
  
          filestream.pipe(res);
  
          return;
      }
  });
  </code></pre>
  
  <p>This looks great, fits right in with the rest of the code and is easy to read, no hacking involved, BUT! It doesn't work! The browser spins and spins and never quite knows what to do. I have ZERO error messages coming up. I can keep using the app on other tabs. I don't know what it's doing, it never stops "loading". If I restart the server, I get a 0 byte file with all the correct headers, but I don't get the data.</p>
  
  <p>Any help is greatly appreciated!!</p>
  
  <p>EDIT:</p>
  
  <p>After digging around a bit more, I noticed that trying to turn the response object into a JSON object results in a circular structure error. </p>
  
  <p>Now the interesting thing about this is that when I listen to the filestream for the "data" event, and attempt to stringify the response object I don't get that error. But if I attempt to do the same thing in my first solution(listen to "data" and stringify the response) I get the error again.</p>
  
  <p>So using the Meteor-Router solution something is happening to the response object. I also noticed that on the "data" event response.finished is flagged as true.</p>
  
  <pre><code>filestream.on('data', function(data) {
      fs.writeFile('/var/MeteorDMS/afterData', JSON.stringify(res));
  });
  </code></pre>