Skip to content
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

Fix "Can't set headers after they are sent" errors #1176

Closed
wants to merge 1 commit into from

Conversation

thiagobustamante
Copy link
Contributor

@thiagobustamante thiagobustamante commented May 26, 2017

This PR fix "Can't set headers after they are sent" errors.
That are a lot of situations where this error can occurs. In my case, it is happening because I have others middlewares (in an expressjs application that tries to proxy requests). Some of those middlewares (like passportjs, or cors) can run res.end() and when the proxy receive a response, it is already finished.
So, it is necessary to test if we can write on the user response when the proxy response is ready.
I think it could also fix #930, #1168, #908 and #867

This PR tries to fix "Can't set headers after they are sent" errors.
That are a lot of situations where this error can occurs. In my case, it is happening because I have others middlewares (in an expressjs application that tries to proxy requests). Some of those middlewares (like [passportjs](http://passportjs.org/), or [cors](https://www.npmjs.com/package/cors)) can run ```res.end()``` and when the proxy receive a response, it is already finished.
So, it is necessary to test if we can write on the user response when the proxy response is ready.
I think it could also fix http-party#930, http-party#1168, http-party#908
@thiagobustamante
Copy link
Contributor Author

Tests are failing in Node.js 0.10.x/0.12.x, but it's unrelated to my changes. See #1167.

if(web_o[i](req, res, proxyRes, options)) { break; }
if (!res.headersSent) {
for(var i=0; i < web_o.length; i++) {
if(web_o[i](req, res, proxyRes, options)) { break; }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a better fix: #1182

Copy link
Contributor Author

@thiagobustamante thiagobustamante Jun 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Frogmella,

I think that your your commit is ok and we should avoid the writeHead function. But if the headers are already sent, does not makes sense to call res.statusCode = proxyRes.statusCode;. The response status code and headers were already sent to client, so you can not modify it anymore.

@jcrugzz
Copy link
Contributor

jcrugzz commented Apr 19, 2018

Thanks for the contribution @thiagobustamante. I will cherry-pick this into my maintenance branch that will be merged in today.

@jcrugzz jcrugzz closed this Apr 19, 2018
@jcrugzz
Copy link
Contributor

jcrugzz commented Apr 20, 2018

superseded by #1251

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can't set headers after they are sent
3 participants