Node: res.setHeader and res.writeHead should return this

Created on 5 Feb 2019  路  2Comments  路  Source: nodejs/node

Is your feature request related to a problem? Please describe.
When working with vanilla node HTTP servers, I frequently make the mistake that res.setHeader and (more often) res.writeHead return this, allowing a .end to be appended. For example:

res.writeHead(201, headers).end(body);

This is an error, since writeHead returns undefined.

Describe the solution you'd like
I'd like to have res.setHeader and res.writeHead return this to allow chaining. The same behaviour should be added to the HTTP/2 compatibility module too. I'm happy to put together a pull request if you are open to the change.

Describe alternatives you've considered
I sometimes define a helper function to do this, but it's clunky.

feature request http

Most helpful comment

Seems like an uncontroversial change. Pull request welcome, I'd say.

All 2 comments

Seems like an uncontroversial change. Pull request welcome, I'd say.

Thanks. I'll work on this this evening (UTC).

Was this page helpful?
0 / 5 - 0 ratings