-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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
Allow passing new Headers()
to res.writeHead(...)
#46082
Comments
@nodejs/http What do you think about this? cc @marco-ippolito, which is interested in implementing this if we choose to opt-in |
SGTM. |
Would maybe also be interesting to explore allowing returning a |
I would be -1 to that. The createServer callback is an event emitted... and adding this would make it exceptionally complicated. |
I like that idea also So maybe also waiting for something to resolve into a response would be useful. But then we should probably have respondWith to have little parity with service worker, as to allow offloading some part of your code off the server onto ServiceWorker to allow the app to work offline. incomingRequest.respondWith(fetch(url))` Talked a little bit about this here: b/c the |
I'm -1 as I don't think this will benefit the ecosystem. The Moreover, I think this kind of compatibility would be better left for higher level frameworks. |
This is one of the major source of promise issues when people are using our http server APIs. Don't do this. |
care to explain why it's an issue to use |
It creates promises when they're not needed. Wastes memory and CPU for no reason. |
This video from @jasnell covers it fairly well: https://m.youtube.com/watch?v=XV-u_Ow47s0. |
I'm also -0.5 as I foresee problems as @mcollina already pointed out. |
I'd be ok in adding a setHeaders() method for example. I just don't want to modify writeHead() or any other low-level APIs. |
I think that's a good compromise. @marco-ippolito Are still interested in implementing it? |
Yes 👍🏻 |
This comment was marked as off-topic.
This comment was marked as off-topic.
if what @ronag suggested in #42529 (comment) becomes available (creating a more ServiceWorker-like server) of having something such as const server = http.listen({ port })
server.addEventListener('fetch', (event) => {
// Prevent the default, and handle the request ourselves.
event.respondWith((async () => {
// Try to get the response from a cache.
const cachedResponse = await caches.match(event.request);
// Return it if we found one.
if (cachedResponse) return cachedResponse;
// If we didn't find a match in the cache, use the network.
return fetch(event.request);
})());
}); |
We have |
If a |
Imho it should support |
On the fence weather or not it should support Object and arrays, All i care about is being able to assign standard Headers i get from another api endpoint. I maybe could have use something like: res.writeHead(200, 'OK', [...headers]) // or
res.writeHead(200, 'OK', Object.fromEntries(headers)) but don't know if it would work... given some headers like maybe: If it should accept anything else other than Headers (such as Object & arrays) then i think it should only accept the same arguments as the Header constructor can accept as an input. res.setHeaders = function (value) {
const headers = new Headers(value)
for (const pair of headers) {
// append headers
}
} that way we can accept both arrays, objects and headers along with something that's iterable such as new Headers(new Map([
['content-type', 'text/plain'],
['content-length', '1024']
])) (headers are also iterable and yields an array of tuples - so otherwise i'm just fine with doing: res.setHeaders( new Headers({foo: 'bar'}) ) Speaking of adding a way of setting headers using the Headers class... should there also be a way to get headers as a |
@jimmywarting |
Yes @marco-ippolito, I agree. @jimmywarting please open a new issue to request the getter. |
What is the problem this feature will solve?
I would like for standard Headers to be passable to http
response.writeHead
An example:
What is the feature you are proposing to solve the problem?
Want things that are more web-ish
What alternatives have you considered?
No response
The text was updated successfully, but these errors were encountered: