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

Can't set headers after they are sent #930

Closed
xolf opened this issue Dec 27, 2015 · 6 comments
Closed

Can't set headers after they are sent #930

xolf opened this issue Dec 27, 2015 · 6 comments

Comments

@xolf
Copy link

xolf commented Dec 27, 2015

I just got a very strange error. The proxy tries to send the header twice and I have no idea why.

_http_outgoing.js:335
    throw new Error('Can\'t set headers after they are sent.');
          ^
Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:335:11)
    at g:\nodejs\http-proxy\node_modules\http-proxy\lib\http-proxy\passes\web-outgoing.js:85:11
    at Array.forEach (native)
    at Array.writeHeaders (g:\nodejs\http-proxy\node_modules\http-proxy\lib\http-proxy\passes\web-outgoing.js:84:35)
    at ClientRequest.<anonymous> (g:\nodejs\http-proxy\node_modules\http-proxy\lib\http-proxy\passes\web-incoming.js:151:20)
    at ClientRequest.emit (events.js:107:17)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:419:21)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:111:23)
    at Socket.socketOnData (_http_client.js:310:20)
    at Socket.emit (events.js:107:17)

My proxy.js file: https://gist.github.com/xolf/f79a1f939d7dc64c9894

@adambiggs
Copy link

+1 I'm having the same problem.

@pyper
Copy link

pyper commented Jan 5, 2016

This situation happens when request() call throws an error. It looks like you have a race condition between your response error function and the web proxy. What is happening is the request() call is failing and the custom error handler and the proxy are fighting over the same response object. res.headersSent will not provide you protection because after both times you use them there is many asynchronous callbacks past them. The stack trace I am getting is indicating the error is originating in the proxy which mean the header flush was likely triggered by res.end() on line 31. This f’d up the proxy since it can no longer write headers itself because the headers were sent there. You have to rethink the fundamental structure of this app to account for the for asynchronous nature of node and http-proxy.

@jasonniebauer
Copy link

+1 I'm having the same problem

@xolf
Copy link
Author

xolf commented Mar 9, 2016

Is there a way to ignore this error and force the server keep on running?

@bubenshchykov
Copy link

+1

@gabrielmancini
Copy link

+1

thiagobustamante added a commit to thiagobustamante/node-http-proxy that referenced this issue May 26, 2017
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
@xolf xolf closed this as completed Apr 11, 2018
jcrugzz pushed a commit that referenced this issue Apr 19, 2018
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 #930, #1168, #908
jcrugzz pushed a commit that referenced this issue Apr 20, 2018
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 #930, #1168, #908
indexzero pushed a commit that referenced this issue Apr 20, 2018
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 #930, #1168, #908
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 a pull request may close this issue.

6 participants