Description
- Version: 6.4
- Platform: Ubuntu 16.04 64-bit
- Subsystem: http
Despite the API documentation's statement that http.ServerResponse.writeHead() "must only be called once on a message", multiple calls to the method simply modify the response header without complaint.
To test this (verified in 6.4, 4.x, and 0.10): Create an instance of http.Server whose request listener writes the response header more than once, then calls write() or end() on the response.
Additionally, calling writeHead() after a write() on the response object will modify the header of any subsequent response messages sent to the client with write(), or cause the server to send one final empty response with the modified head if end() is called without any additional messages being sent.
Example code:
require('http').createServer(function(req,res){res.writeHead(500,'Internal Server Error');res.write('Hello World!');setImmediate(function(){res.writeHead(200);res.end();});}).listen(3000);
I'm not sure if this is unintended behavior or simply misleading naming/documentation, but one might assume that the first call to writeHead() would cause any subsequent calls to either throw an error or fail silently.