【问题标题】:Response not ending and browser keeps loading - nodejs and graphicsmagick响应没有结束并且浏览器不断加载-nodejs和graphicsmagick
【发布时间】:2014-07-15 16:13:22
【问题描述】:

我是 nodejs 的新手。在将图像发送到浏览器之前,我正在使用 graphicsmagick 来调整图像大小。 我的代码看起来像这样(res 是从 function(req,res){...} 发送的响应)-

imageURLPromise
.then(function(response) {
    var  body = response.body;
    if (body) {
    console.log("Found From: " + response.request.uri.href);

    //set response headers here
    setResponseData(res, response);


    var buf = new Buffer(body, "binary");
    console.log(buf);
    //gm module
    gm(buf).resize(400,300).toBuffer(function(err,buffer) {
        console.log("buffer here");
        res.end(buffer, "binary");
    });
    }
}, function(error) {
      console.log(error);
});

我在浏览器中获取图像,我在此处获取日志“缓冲区”,但浏览器仍处于“加载”状态。

我曾尝试将 .stream() 与 gm 一起使用并将标准输出通过管道传输到响应,但它有同样的问题。

如果我取消 gm 并像这样直接将正文写入响应

res.end(body, 'binary');

然后它就可以正常工作了。

谁能告诉我这里做错了什么?

【问题讨论】:

    标签: node.js imagemagick graphicsmagick


    【解决方案1】:

    我发现了问题所在。 问题不在于 node 或 gm,而在于 HTTP 响应标头。

    当 GM 返回一个缓冲区并且我们将其写入 HTTP 响应时,它会将 Transfer-Encoding 标头设置为“分块”。在这种情况下,不应设置 Content-Length 标头。 你可以在这里读更多关于它的内容 http://en.wikipedia.org/wiki/Chunked_transfer_encoding

    由于我同时设置了这两个浏览器,即使在图像发送后,浏览器也会一直等待内容。

    代码与我发布的完全相同,除了在 setResponseData() 函数(主要用于设置标题)中,我现在没有设置 content-length 标题。

    【讨论】:

      猜你喜欢
      • 2014-07-27
      • 2012-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-26
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      相关资源
      最近更新 更多