3

I have been running Node 0.8.25 and Express 3.4.0.

Works great.

I tried upgrading to Node 0.10 Stable and I started having strange problems.

For example, I have a route that looks (sort of) like this:

app.post( "/toPDF", auth.isAuthorized, function( req, res, next ){

    getPDF( req.data, function( err, pdfData ) {
        if ( err ) {
            next( err );
        } else {
            res.setHeader( "Content-type", "application/pdf" );
            res.setHeader( "Cache-Control", "no-cache" );
            res.setHeader( "Accept-Ranges", "none" );
            res.setHeader( "Content-Disposition", "inline; filename=stuff.pdf" );
            res.end( pdfData.raw, "binary" );
        }
    });

});

After upgrading to Node 0.10, the first time I call this route, everything works as expected.

However the second time the route is called, the application crashes with the error "Headers already sent."

I wanted to see which headers were already present in the response. So I added the following to the top of the getPDF callback, before any headers are sent.

console.log( res._headers );

Right before it crashes, this outputs:

Content-type application/pdf
Cache-Control no-cache
Accept-Ranges none
Content-Disposition inline; filname = stuff.pdf

It looks as if my old response object is somehow hanging around? Any ideas what might be going on here?

user1031947
  • 6,294
  • 16
  • 55
  • 88
  • 1
    https://github.com/joyent/node/wiki/Api-changes-between-v0.8-and-v0.10 – Benjamin Gruenbaum Sep 22 '13 at 14:55
  • @Benjamin - I am not clear from reading those changes what I am doing wrong. – user1031947 Sep 22 '13 at 15:07
  • I did not imply that this solves your problem (I couldn't see it based on the info you provided either, and I think it's interesting - hence the upvote). I just thought I'd comment with this as this is a good place to start. – Benjamin Gruenbaum Sep 22 '13 at 15:18
  • 1
    Could you show a stack trace and more of your code? There is nothing wrong with the code that you've shown. – hexacyanide Sep 22 '13 at 15:24
  • Based on other answers (http://stackoverflow.com/questions/7372667/how-to-debug-headers-sent-errors-in-node-js), are you sure you aren't calling `next()` twice? Does your auth code call it? Are we hitting the error block and also causing that to hit `next`? – Tony Sep 24 '13 at 22:13
  • @Tony - I don't think so... I have gone through all of the auth code with a fine-tooth comb, and I cannot see any way that next would be called more than once. I have rolled back to Node 0.8.25 and it is working fine again. Very strange. – user1031947 Sep 26 '13 at 15:21
  • According to this other [stackoverflow thread](http://stackoverflow.com/questions/7042340/node-js-error-cant-set-headers-after-they-are-sent) The error means that you're already in the Body or Finished state, but some function tried to set a header or statusCode. When you see this error, try to look for anything that tries to send a header after some of the body has already been written. For example, look for callbacks that are accidentally called twice, or any error that happens after the body is sent. Can you post auth.isAuthorized function to see if you are setting any headers there. – Pradeep Mahdevu Sep 30 '13 at 17:40

1 Answers1

0

It is possible that you called next() twice somewhere in your middleware - check auth.isAuthorized or any other you are using. As described in this question.

Community
  • 1
  • 1
eleventigers
  • 101
  • 1
  • 4