我无法在meteorjs 中使用imagemagick。我正在开发一个小型 svg->png 转换器,其中包含一个用于提供转换后的图像的 rest api。我用meteor-router实现了其余的api。imagemagick 转换有效。但是,我无法将转换结果写入 http 响应。我试图通过使用光纤来消除异步性来解决这个问题。但这仍然行不通。基本上,所有 request.write 调用在 yield 执行后都会被忽略。这是我的代码:
Meteor.Router.add({
'/image/:hash' : function(hash) {
var svg = Images.findOne({'hash' : hash}).svg;
var request = this.request;
var response = this.response;
Fiber(function() {
var fiber = Fiber.current;
response.writeHead(200, {'Content-Type':'image/png'});
var convert = imagemagick.convert(['svg:-', 'png:-']);
convert.on('data', function(data) {
response.write("doesn't work");
//response.write(data);
});
convert.on('end', function() {
response.write("doesn't work");
//response.end();
fiber.run({});
});
convert.stdin.write(svg);
convert.stdin.end();
response.write("works");
Fiber.yield();
response.write("doesn't work");
}).run();
}
});
我对meteorjs很陌生。因此,我可能会完全错误地使用 Fiber。或者我根本不应该使用纤维。有人可以帮忙吗?