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' with express #953

Open
jnjnljljjsw opened this issue Feb 4, 2016 · 6 comments
Open

'Can't set headers after they are sent' with express #953

jnjnljljjsw opened this issue Feb 4, 2016 · 6 comments

Comments

@jnjnljljjsw
Copy link

my node.js version is 4.2.2

app.js:
var api = require('./routes/api'); app.use('/api', api);

api.js
proxy.on('proxyReq', function(proxyReq, req, res, options) { proxyReq.setHeader('X-Special-Proxy-Header', 'foobar'); }); router.all("/*", function(req, res){ proxy.web(req, res, { target: 'http://dev.lalocal.cn:8080/api' }); });

error:
Error: Can't set headers after they are sent.

@michaellujan
Copy link

Having the same issue under load. My set up is almost identical

@tomcanham
Copy link

I ran into this too; it only seems to happen for multipart (chunked) responses. I didn't dig into it too much, but I found a workaround. When you call the proxy method, you pass in an options object -- the third parameter, which contains the target.

Well apparently, if this object contains a 'headers' object, those headers are copied to the proxied request as it's being initialized, and the timing of this copy avoids whatever race condition is causing this failure.

From my code:

              options = { target: target }

              if (result.user) {
                options.headers = {}

                options.headers['x-auth-user-id'] = result.user._id
                options.headers['x-auth-user-username'] = result.user.username
                options.headers['x-auth-user-roles'] = result.user.roles
              }

              return proxy.web(req, res, options)

@bubenshchykov
Copy link

+1, happens during bigger load

@phris
Copy link

phris commented Apr 8, 2017

+1

@michaellujan
Copy link

not sure your exact use case but I ended up writing something that handled this better. Have used it in production for over a year handling multi millions of requests, and am now open sourcing it:

BiFrost

@gabrielmancini
Copy link

+1

jakefurler pushed a commit to jakefurler/node-http-proxy that referenced this issue Jun 14, 2017
object.keys in web-incoming.js results in a non-deterministic ordering of keys, which means that in web-outgoing writeHead might be called before setHeader, which throws an error
jcrugzz pushed a commit that referenced this issue Apr 19, 2018
object.keys in web-incoming.js results in a non-deterministic ordering of keys, which means that in web-outgoing writeHead might be called before setHeader, which throws an error
jcrugzz pushed a commit that referenced this issue Apr 20, 2018
object.keys in web-incoming.js results in a non-deterministic ordering of keys, which means that in web-outgoing writeHead might be called before setHeader, which throws an error
indexzero pushed a commit that referenced this issue Apr 20, 2018
object.keys in web-incoming.js results in a non-deterministic ordering of keys, which means that in web-outgoing writeHead might be called before setHeader, which throws an error
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

No branches or pull requests

6 participants