Replies: 1 comment 3 replies
-
When the response is async I use these functions to gather the Status and Header values from different middleware and write them at the end, because the Status needs to be written before the headers and you want to cork to write status, headers, and body at same time: const setStatus = (res, status) => res.status = status;
const setHeader = (res, name, value) => (res.headers ??= []).push(name, value);
const end = (res, body, status = res.status) => !res.ab && res.cork(() => {
if (status) res.writeStatus(status);
if (res.headers) for (let i = 0; i < res.headers.length;) res.writeHeader(res.headers[i++], res.headers[i++]);
res.end(body);
}); usage: app.get('/', async res => {
res.onAborted(() => res.ab = true);
await doAsync();
setHeader(res, 'Set-Cookie', 'name=value');
await doAsync();
end(res, 'body', '304');
}); |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Applying corking is quite affecting my current application architecture as it's no longer allowed to call
res.writeHeader
within the services. Instead, I need to collect all headers then send them all at once withinres.cork
. But collecting multipleSet-Cookie
headers will still be problematic though.Did I misunderstood the feature or its usage? Otherwise, do you have a recommendation for me in this case?
Beta Was this translation helpful? Give feedback.
All reactions