【问题标题】:Iron router on action using Meteor.call throws writeHead is not a functionIron router on action using Meteor.call throws writeHead is not a function
【发布时间】:2016-05-24 10:59:32
【问题描述】:

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

因此,我定义了一个路由,用于在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);
        }
    }
});

这会抛出:

调用“downloadFile”的结果传递异常:TypeError: that.writeHead 不是函数

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

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

提前致谢

【问题讨论】:

  • 你为什么不尝试设置var that = thisthat.response.writeHead(...)
  • 它也没有帮助

标签: javascript node.js meteor iron-router


【解决方案1】:

您是否尝试过在 IR 中使用仅服务器端的路由?即仅使用 `{where:"server"}' 使路由在服务器上可访问,以避免必须按照下面的示例从here 进行方法调用(注意在他的示例中提供文件需要添加meteorhacks:根据 cmets 的 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"
});
猜你喜欢
  • 1970-01-01
  • 2021-04-22
  • 1970-01-01
  • 2020-11-08
  • 2013-10-20
  • 1970-01-01
  • 1970-01-01
  • 2022-12-26
  • 1970-01-01
相关资源
最近更新 更多