Node.js:如何禁用chunked transfer-encoding?

时间:2014-07-14 03:58:48

标签: node.js express http-headers

我错过了来自节点服务器的响应的content-length标题,我从另一个位置管道.zip文件。我已经通过下面的代码注入了content-length标题,但似乎transfer-encoding: chunked仍以某种方式覆盖它。

响应标头

HTTP/1.1 200 OK
access-control-allow-origin: *
connection: close
content-type: application/zip
date: Mon, 14 Jul 2014 03:47:00 GMT
etag: "\"eb939974703e14ee9f578642972ed984\""
last-modified: Sat, 12 Jul 2014 02:15:52 GMT
server: Apache-Coyote/1.1
set-cookie: rememberMe=deleteMe; Path=/; Max-Age=0; Expires=Sun, 13-Jul-2014 03:47:00 GMT
transfer-encoding: chunked
X-Powered-By: Express

代码

var request = require('request');
var express = require('express');
var async = require('async');

var app = express();

app.get('/:bundle_id?', function(req, res) {
    var bundle_id = req.params.bundle_id;
    bundle_id = bundle_id.replace(/\.zip$/, '');

    var url = "https://url....../bundles/" + bundle_id;

    async.waterfall([

        function(callback) {
            request.get(url, function(req, res, data) {
                callback(null, JSON.parse(data).entities[0]['file-metadata']['content-length']);
            });
        }
    ], function(err, contentLength) {

        request.get({
            url: url,
            headers: {
                "Accept": "application/zip"
            }
        }).pipe(res);

        res.oldWriteHead = res.writeHead;
        res.writeHead = function(statusCode, reasonPhrase, headers) {
            res.header('Content-Length', contentLength);
            res.oldWriteHead(statusCode, reasonPhrase, headers);
        }
    });
});

app.listen(9000);

1 个答案:

答案 0 :(得分:10)

原来这实际上是一个相当简单的修复:在响应中将transfer-encoding标头设置为空字符串解决了问题:

...
res.oldWriteHead = res.writeHead;
res.writeHead = function(statusCode, reasonPhrase, headers) {
    res.header('Content-Length', contentLength);
    res.header('transfer-encoding', ''); // <-- add this line
    res.oldWriteHead(statusCode, reasonPhrase, headers);
}
...

这可行的原因是因为在doing some digging之后,transfer-encoding标题会替换content-length(因为两者无法共存)。事实上,我用来测试的客户端是在内容长度上选择分块传输编码。