Node.js 设置了错误的内容长度。我该如何设置这个
Node.js set wrong content length. how do i set this one
我需要一些帮助。即使我将内容长度设置为响应对象,它似乎也不起作用。我是不是做错了什么?
res.set({
'Content-Type': res._data.ContentType,
'Content-Length': res._data.ContentLength,
ETag: res._data.ETag
});
res.send(res._data.Body);
我使用express和body-parser发送数据,我设置了Content-Length。
res.header('Content-Type', res._data.ContentType);
res.header('content-length', res._data.ContentLength);
res.header('Cache-Control', 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0');
这个我也试过了
{ AcceptRanges: 'bytes',
LastModified: 'Thu, 15 Jan 2015 22:23:43 GMT',
ContentLength: '648789',
ETag: '"7da971554e6ff4f6dfcdb7b2ba5e0be3"',
ContentType: 'image/jpeg',
Metadata: {},
Body: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 01 00 48 00 48 00 00 ff db 00 43 00 08 06 06 07 06 05 08 07 07 07 09 09 08 0a 0c 14 0d 0c 0b 0b 0c 19 12 13 0f 14 ...> }
这是 console.log(res._data)
的结果。如您所见,我设置了内容长度。
GET /api/images/54b83dec5e7d97c546e0c41a 200 584.524 ms - 1227943
但是 node.js 向客户端响应错误的内容长度,大小为 1227943。
Cache-Control:no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0
Connection:keep-alive
content-length:1227943
Content-Type:image/jpeg
Date:Thu, 15 Jan 2015 23:37:52 GMT
ETag:"7da971554e6ff4f6dfcdb7b2ba5e0be3"
X-Powered-By:Express
客户也收到了。有人知道我错过了什么吗?
请帮助我。
哦忘了说了。当我使用 Postman(chrome extension) 获取数据时,它工作正常。
GET /api/images/54b83dec5e7d97c546e0c41a 200 664.427 ms - 648789
所以我真的不知道为什么会这样。
当您使用 Express 的 response.send
方法时,您无法手动设置 Content-Length
:该方法所做的最后一件事是根据(令人惊讶的)长度设置 Content-Length
发送的内容。可以看到在源码中send
方法的最后:
https://github.com/strongloop/express/blob/4.11/lib/response.js
换句话说,Express 优先于 Content-Length
。
看起来 res._data.ContentLength
没有反映缓冲区的大小 res._data.Body
。查看 res._data.Body.length
是什么;我猜是 1227943。
我需要一些帮助。即使我将内容长度设置为响应对象,它似乎也不起作用。我是不是做错了什么?
res.set({
'Content-Type': res._data.ContentType,
'Content-Length': res._data.ContentLength,
ETag: res._data.ETag
});
res.send(res._data.Body);
我使用express和body-parser发送数据,我设置了Content-Length。
res.header('Content-Type', res._data.ContentType);
res.header('content-length', res._data.ContentLength);
res.header('Cache-Control', 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0');
这个我也试过了
{ AcceptRanges: 'bytes',
LastModified: 'Thu, 15 Jan 2015 22:23:43 GMT',
ContentLength: '648789',
ETag: '"7da971554e6ff4f6dfcdb7b2ba5e0be3"',
ContentType: 'image/jpeg',
Metadata: {},
Body: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 01 00 48 00 48 00 00 ff db 00 43 00 08 06 06 07 06 05 08 07 07 07 09 09 08 0a 0c 14 0d 0c 0b 0b 0c 19 12 13 0f 14 ...> }
这是 console.log(res._data)
的结果。如您所见,我设置了内容长度。
GET /api/images/54b83dec5e7d97c546e0c41a 200 584.524 ms - 1227943
但是 node.js 向客户端响应错误的内容长度,大小为 1227943。
Cache-Control:no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0
Connection:keep-alive
content-length:1227943
Content-Type:image/jpeg
Date:Thu, 15 Jan 2015 23:37:52 GMT
ETag:"7da971554e6ff4f6dfcdb7b2ba5e0be3"
X-Powered-By:Express
客户也收到了。有人知道我错过了什么吗? 请帮助我。
哦忘了说了。当我使用 Postman(chrome extension) 获取数据时,它工作正常。
GET /api/images/54b83dec5e7d97c546e0c41a 200 664.427 ms - 648789
所以我真的不知道为什么会这样。
当您使用 Express 的 response.send
方法时,您无法手动设置 Content-Length
:该方法所做的最后一件事是根据(令人惊讶的)长度设置 Content-Length
发送的内容。可以看到在源码中send
方法的最后:
https://github.com/strongloop/express/blob/4.11/lib/response.js
换句话说,Express 优先于 Content-Length
。
看起来 res._data.ContentLength
没有反映缓冲区的大小 res._data.Body
。查看 res._data.Body.length
是什么;我猜是 1227943。