Iron router on action using Meteor.call 抛出 writeHead 不是函数

Iron router on action using Meteor.call throws writeHead is not a function

我正在尝试写一个缓冲区,returns 从服务器到客户端的响应。

因此,我定义了一个路由,该路由在 action 函数上运行以获取文件:

Router.route('/download/:blabla', {
    name: 'download',
    action: function () {
       var that = this.response;
       Meteor.call('downloadFile', blabla, function (err, result) {
           // error check etc.
           var headers = {
               'Content-type' : 'application/octet-stream',
               'Content-Disposition' : 'attachment; filename=' + fileName
           };
           that.writeHead(200, headers);
           that.end(result);
        }
    }
});

这会抛出:

Exception in delivering result of invoking 'downloadFile': TypeError: that.writeHead is not a function

没有 Metoer.call 它可以工作...

我正在使用 nodejs 流在服务器端函数上获取缓冲区并且它有效。

提前致谢

您是否尝试过在 IR 中仅使用服务器端路由?也就是说,使路由只能在服务器上使用“{where:"server"}”访问,以避免必须按照下面的示例从 here 进行方法调用(注意在他的示例中提供文件需要添加meteorhacks:npm 包的评论,但你可以避免这种情况......):

Router.route("/file/:fileName", function() {
  var fileName = this.params.fileName;

  // Read from a file (requires 'meteor add meteorhacks:npm')
  var filePath = "/path/to/pdf/store/" + fileName;
  var fs = Meteor.npmRequire('fs');
  var data = fs.readFileSync(filePath);

  this.response.writeHead(200, {
    "Content-Type": "application/pdf",
    "Content-Length": data.length
  });
  this.response.write(data);
  this.response.end();
}, {
  where: "server"
});