-
Notifications
You must be signed in to change notification settings - Fork 29.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
http: Add OutgoingMessage#addHeader #33835
Conversation
Perhaps |
@mscdex |
I would prefer not to further expand on the http api. @mcollina wdyt? |
Overall I agree with @ronag. I would say this is handled by frameworks in most cases. What does fellow @nodejs/web-server-frameworks members think? |
I also agree with @ronag. We should only expand the API if the use case is frequent enough and there are no reasonable alternatives. That is not the case here, as alternatives are already provided by most frameworks. |
I would prefer not to monkey-patch Node.js core code; and I'm not sure how else you'd solve the problem I'm getting at here. |
The web frameworks don't patch, they wrap in their own req/res objects. |
I tend to use modular libraries in lieu of frameworks... For example, I have this: https://github.com/awwright/node-http-transform/blob/master/lib/Pair.js — It creates a related pair of HTTP message streams, e.g. a writable Even for frameworks though, why should userland ever have to worry about the current value of the header just to append a new one? |
@mcollina It actually looks like there's an item on the docket for this problem, nodejs/web-server-frameworks#32 I'm curious why "Is this not handled well by any existing frameworks" ought to be the bar to pass? Do the other implicit header functions meet this test? I would like to suggest a different test: Userspace shouldn't have to read the existing buffered headers before calling an implicit header function. For example, headers that are only specified once (e.g. Likewise, for headers that may be specified multiple times, they shouldn't have to make a call to see if headers are already defined or not, they should just be able to make an appropriate implicit header call. A separate function for adding cookies, as was proposed, would not pass this test; I agree this is appropriate for a module instead (except to the extent cookies don't follow the standard HTTP syntax, but Node.js already handles this much). |
I think EDIT: Adding a spec compliant |
This comment has been minimized.
This comment has been minimized.
Closing this as stale. |
On occasion I've needed to append an HTTP header instead of set one. For example, Link and Set-Cookie often specify message headers in multiple places, all of which need to appear in the message.
Doing this with
setHeader
is awkward, requiring three different branches, depending on the current value of the header. Here is a simple polyfill:It seems to me that userland code shouldn't have to concern itself with the current state of the headers if it doesn't need to, therefore, there should be a function that adds a new header (in addition to any existing).
I don't have documentation started yet, but this is a fairly straightforward feature; and this needs an http2 implementation. I can get started on those, if this feature looks reasonable.
Note this will re-use the capitalization of the existing header, if it exists.
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes