question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use of `res.end` is preventing other middleware from completing in Node

See original GitHub issue

I’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:

https://github.com/apollographql/apollo-server/blob/a3282a2d7df0c20d9e10b058defae835120fa5b1/packages/apollo-server-express/src/ApolloServer.ts#L202

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:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
davedashcommented, Mar 24, 2021

@glasser you rock.

I’m posting this - just on the off chance someone runs into the same issues as I.

  1. I created the code sandbox per your suggestion. The minimal implementation I thought should fail, did not.
  2. So I pulled that all into my own repo and of course it failed for me.
  3. So I dug into the details, and it seemed some packages were slightly different.
  4. Upgraded a few packages via yarn
  5. It works locally.

Thanks for the suggestion, it really helped me narrow it down quickly.

1reaction
glassercommented, Mar 24, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found