1

For some reason, if I'm the last piece of middleware and I try calling the next() function, I get the same error as this common SO issue.

So that I have a minimal example, I set it up as follows:

In my route, I have:

router.get('/add',(req,res,next) => {
    res.render('ideas/add');
    next();
});

And then I have a final piece of middleware:

app.use(function finalMiddleware(req,res,next){
    console.log("We are here!");
    console.log(app._router.stack);
    next();
});

And the console logs the following as the last element in the stack:

  Layer {
    handle: [Function: finalMiddleware],
    name: 'finalMiddleware',
    params: {},
    path: '',
    keys: [],
    regexp: { /^\/?(?=\/|$)/i fast_star: false, fast_slash: true },
    route: undefined } ]

However, I still get:

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:471:11)
    at ServerResponse.header (../node_modules/express/lib/response.js:767:10)
    at ServerResponse.send (../node_modules/express/lib/response.js:170:12)
    at done (../node_modules/express/lib/response.js:1004:10)
    at Immediate.<anonymous> (../node_modules/express-handlebars/lib/utils.js:26:13)

I'm wondering if this for some reason has to do with handlebars? I just don't understand why if I'm not writing headers with later middleware that this would possibly be an issue. I can't seem to figure out how to debug it further. Sure, I can remove the next() call from my final piece of middleware, but I want to understand if there's something deeper going on as I'm new to Express and want to make I'm understanding stuff correctly.

rb612
  • 5,280
  • 3
  • 30
  • 68
  • 1
    possible duplicate of https://stackoverflow.com/q/7042340/3461055 – Arif Khan May 21 '18 at 04:53
  • @ArifKhan no, I linked that in my description. – rb612 May 21 '18 at 04:56
  • Please read carefully, issue is same, `res.render` finished the response, you can not call next or another middleware after `res.render` – Arif Khan May 21 '18 at 05:00
  • @ArifKhan, you're right. Sorry, I didn't understand at first. I figured it out. Does middlware that's not in a router callback, when calling `next()` will send a `Cannot GET` message also? Or is it just if it's called in a router callback? – rb612 May 21 '18 at 05:05
  • 1
    Possible duplicate of [Error: Can't set headers after they are sent to the client](https://stackoverflow.com/questions/7042340/error-cant-set-headers-after-they-are-sent-to-the-client) – Ankur Anand May 21 '18 at 05:06

1 Answers1

1

It turns out I was missing a key piece of information from both the similar answer and from my investigation: if next() is called and nothing is left to execute in regards to middlware, the server will send a message Cannot GET /(route name). For me, my callback was executing after this message was sent, since next() must call the function that writes this default response.

Hence, since Express already sent the message Cannot GET /(routename), as soon as the callback fired, which was after, it wasn't able to send a response.

rb612
  • 5,280
  • 3
  • 30
  • 68