我在一个小项目中使用了node.js和express。我设置响应头如下:
res.set({'Content-Type':'text/plain;charset=utf-8',
'Content-Length': Buffer.byteLength(data, 'utf-8')});
我可以使用 console.log
打印数据长度为317。但是在浏览器控制台中,我只能看到以下内容:
Connection:keep-alive
Content-Encoding:gzip
Content-Type:text/plain;charset=utf-8
Date:Sat, 01 Jun 2013 08:21:59 GMT
Transfer-Encoding:chunked
Vary:Accept-Encoding
X-Powered-By:Express
那么,为什么content-length消失了呢?
app.use(express.compress());
后,content-length 显示出来了。但是,我可以同时使用 gzip 和 content-length 吗? - Allen Heaveyconnect.compress
不会在响应中添加新的Content-Length
的原因是它需要缓冲压缩后的响应体。只有当缓冲区完整时,才能知道内容长度。 - Myrne Stolzlib.gzip
自己压缩缓冲区,然后执行res.set({"Transfer-Encoding": "gzip"})
,那么connect.compress
中间件将不会处理它。 - Myrne Stol