-->

标准输出与节点的服务穿过二进制图像问题(Problems serving a binary imag

2019-07-29 01:07发布

我试图创建,可供使用的节点wkhtml模块(基本上只为wkhtmltoimage / wkhtmltopdf命令行实用程序的包装)中产生一个PNG图像中的节点服务器。 这是我到目前为止有:

var http = require('http');
var Image = require("node-wkhtml").image();

http.createServer(function (request, response) {
  new Image({ url: "www.google.com" }).convert (function (err, stdout) {
    //var theImage = new Buffer (stdout, 'binary');
    response.writeHead(200, {'Content-Type' : 'image/png',
                           'Content-Length' : stdout.length
    });
    response.write (stdout, 'binary');
    response.end ();
    //write out an error, if there is one
    if (err)
      console.log (err);
  });
}).listen(8124);

基本上,模块调用命令:

wkhtmltoimage www.google.com -

然后产生一个PNG图像,并将其写入到标准输出。 提供服务的数据量似乎是正确的,但我不能让浏览器显示它(也不如果我下载一个文件,它的工作原理)。 我想下面的命令:

wkhtmltoimage www.google.com - > download.png

果然,download.png创建并包含在谷歌网页快照,这意味着wkhtmltoimage实用程序正常工作,并且命令的工作。 我在节点的初学者,所以我不熟悉的超级如何服务了这样的二进制文件,任何人都可以看到任何明显的问题是什么? 下面是工作的魔术节点模块代码:

Image.prototype.convert = function(callback) {    
  exec(util.buildCommand(this), {encoding: 'binary', maxBuffer: 10*1024*1024}, callback);
}

(该buildCommand函数只是产生“wkhtmltoimage www.google.com - ”命令,我已经验证它正确地做到这一点,利用节点检查。

更新:如果有人发现了这个以后,是有兴趣的,我找到了解决办法。 我使用该插件(节点wkhtml)没有正确地处理大的缓冲区,由于使用的选择child-process.exec我改为使用插件代码child-process.spawn相反,它的工作达到目标。

文章来源: Problems serving a binary image passed through stdout with Node