Javascript 使用Meteor.call运行的Iron路由器抛出writeHead不是一个函数
我正在尝试编写一个从服务器返回到客户端响应的缓冲区 因此,我定义了一个路由,它在Javascript 使用Meteor.call运行的Iron路由器抛出writeHead不是一个函数,javascript,node.js,meteor,iron-router,Javascript,Node.js,Meteor,Iron Router,我正在尝试编写一个从服务器返回到客户端响应的缓冲区 因此,我定义了一个路由,它在action函数上运行以获取文件: Router.route('/download/:blabla', { name: 'download', action: function () { var that = this.response; Meteor.call('downloadFile', blabla, function (err, result) {
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不是一个函数
没有我,我认为它是有效的
我正在使用nodejsstream在服务器端函数上获取缓冲区,它可以正常工作
提前感谢您是否尝试在IR中使用仅服务器端路由?i、 e.仅使用`{where:“server”}在服务器上访问路由,以避免按照下面的示例进行方法调用(注意,在该示例中提供文件需要根据注释添加meteorhacks:npm包,但您可能能够避免这种情况……:
为什么不尝试设置
var that=this
和that.response.writeHead(…)
它也没有帮助
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"
});