Use of `res.end` is preventing other middleware from completing in Node
See original GitHub issueI’m running into an issue where I use express-openid-connect
and both that package and ApolloServer conflict:
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at new NodeError (node:internal/errors:329:5)
at ServerResponse.setHeader (node:_http_outgoing:573:11)
at ServerResponse.header (/Users/davedash/Projects/backend/node_modules/express/lib/response.js:771:10)
at ServerResponse.append (/Users/davedash/Projects/backend/node_modules/express/lib/response.js:732:15)
at ServerResponse.res.cookie (/Users/davedash/Projects/backend/node_modules/express/lib/response.js:857:8)
at setCookie (/Users/davedash/Projects/backend/node_modules/express-openid-connect/lib/appSession.js:135:13)
at CookieStore.set (/Users/davedash/Projects/backend/node_modules/express-openid-connect/lib/appSession.js:150:7)
at ServerResponse.resEnd [as end] (/Users/davedash/Projects/backend/node_modules/express-openid-connect/lib/appSession.js:295:21)
at /Users/davedash/Projects/backend/node_modules/apollo-server-express/dist/ApolloServer.js:135:25
at Layer.handle [as handle_request] (/Users/davedash/Projects/backend/node_modules/express/lib/router/layer.js:95:5)
Not 100% sure what’s happening, but it seems like some cookies are trying to be set by express-openid-connect
and maybe that middleware is running at the same time as the playground code. So when we get to here:
We end the response and express gives us the above error.
I was able to temporarily fix this by changing that line to res.send(playground)
and removing the res.write
and res.end
calls.
Unfortunately, since I don’t know what’s really happening, I don’t feel confident publishing that as a pull request - and I’m wondering if there’s a better way to handle this.
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
how to stop middleware chain? - node.js - Stack Overflow
1) You can send a response and not call next() at all. · 2) You can do just plain next() which advances to...
Read more >How Node JS middleware Works? - Selvaganesh - Medium
End the request-response cycle. Call the next middleware in the stack. If the current middleware function does not end the request-response cycle, it...
Read more >How to Build Middleware for Node.js: A Complete Guide - Turing
When the active middleware function doesn't stop the request-response cycle, it will call the next() function to pass on the control to the...
Read more >Error handling - Express.js
It's important to ensure that Express catches all errors that occur while running route handlers and middleware. Errors that occur in synchronous code ......
Read more >Top 10 Most Common Node.js Developer Mistakes - Toptal
In Node.js, callbacks used to be the only way asynchronous elements of your code communicated with each other - up until promises were...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@glasser you rock.
I’m posting this - just on the off chance someone runs into the same issues as I.
Thanks for the suggestion, it really helped me narrow it down quickly.
It sounds like perhaps the way you’ve integrated express-openid-connect and apollo-server-express conflict with each other? Perhaps you need to make sure that the other package runs before Apollo Server or only when Apollo Server handlers aren’t running? I won’t be able to help debug without actually being able to reproduce the issue on my own machine. I’ll be happy to reopen this issue if you provide a full reproduction that doesn’t require me to be creative at all to try it out: a series of instructions (probably including
git clone
or using something on codesandbox.io) that shows the issue to me.