Iron router on Action 使用 Meteor.call 抛出 writeHead 不是一个函数

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

本文关键字:函数 一个 writeHead Action on router 使用 Meteor 抛出 call Iron      更新时间:2023-09-26

我正在尝试编写一个缓冲区,该缓冲区从服务器返回到客户端响应。

因此,我定义了一个路由,该路由运行以在函数上获取文件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);
        }
    }
});

这将引发:

调用"下载文件"的传递结果异常:类型错误: that.writeHead 不是一个函数

没有Metoer.call它工作...

我正在使用 nodejs 流在服务器端函数上获取缓冲区,它可以工作。

提前致谢

您是否尝试过在 IR 中使用仅限服务器端的路由? 即仅使用"{where:"server"}"在服务器上访问路由,以避免必须按照下面的示例从这里进行方法调用(注意在他的示例中提供文件需要根据注释添加 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"
});