Serving HTTP/1.0 responses with Node.JS (unknown content length, chunked transfer encoding)

杀马特。学长 韩版系。学妹 提交于 2019-12-01 02:45:02

Forcing non-chunked responses, the right way

There's a supported way of turning off chunked encoding: you simply need to remove the Transfer-Encoding header using request.removeHeader(name):

response.removeHeader('transfer-encoding');

Node.js will respect no matter what. There's even a test to prevent someone from accidentally changing this behavior, so I think it's pretty safe to use.

So you can just stick with attempt #1, but doing it as described above.

For my purposes, I found an easy way to disable the forced usage of chunked, using an undocumented property of the response object:

response.useChunkedEncodingByDefault = false;

It's that simple. Of course, relying on this property to be available for future versions of Node.js isn't the best. Perhaps there is a better solution, but this works for me for now.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!