question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error: Can't set headers after they are sent. (Express 4)

See original GitHub issue

I am getting this when using with express 4 and express-flash.

I used with swig and all worked.

Here is failing when render is called, it return the html and then fails.

Wed, 27 Jan 2016 21:41:57 GMT connect:redis GET "5cg6LbHdXhWuUnKXYWL6kPNRfeN1TN9u"
Wed, 27 Jan 2016 21:41:57 GMT connect:redis SET "yWgxdkWw0rTHBpi-RuzqRkYLbOUQGdYG" {"cookie":{"originalMaxAge":null,"expires":null,"httpOnly":true,"path":"/"},"flash":{}} ttl:86400
_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 ServerResponse.header (/Users/carlitux/Projects/project/node_modules/express/lib/response.js:718:10)
    at ServerResponse.send (/Users/carlitux/Projects/project/node_modules/express/lib/response.js:163:12)
    at done (/Users/carlitux/Projects/project/node_modules/express/lib/response.js:957:10)
    at /Users/carlitux/Projects/project/node_modules/nunjucks/src/environment.js:22:23
    at RawTask.call (/Users/carlitux/Projects/project/node_modules/asap/asap.js:40:19)
    at flush (/Users/carlitux/Projects/project/node_modules/asap/raw.js:50:29)
    at doNTCallback0 (node.js:419:9)
    at process._tickCallback (node.js:348:13)

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:18 (1 by maintainers)

github_iconTop GitHub Comments

212reactions
carljmcommented, Jan 27, 2016

I don’t use Express, so I probably won’t be able to be much help here. Usually that kind of error is because something is sending output to the client earlier than expected? I don’t have any idea why Nunjucks would be different from Swig in that regard, though.

21reactions
xingbofengcommented, Mar 15, 2017

because you might send no less than 2 times response.

like my code:

  getAllTables().then((data) => res.send(data)).catch((err) => console.log(err));
  next();

I used Promise.then() and my function next() goes before my res.send(), so I had response more than 1 times to front-end. like me, you can write like this.

  getAllTables().then((data) => {
    res.send(data);
    return next();
  }).catch((err) => console.log(err));
Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot set headers after they are sent to the client | End Your If
Why am I receiving the error message can't set headers after they are sent to the client Nodejs? · Calling the request handler...
Read more >
Resolving HTTP Cannot set headers after they are sent ... - Fjolt
This is quite a confusing error if you aren't familiar with HTTP headers. This error arises when you send more than 1 responses...
Read more >
Cannot set headers after they are sent to the client in JS
The "Cannot set headers after they are sent to the client" error occurs when the server in an express.js application sends more than...
Read more >
Understanding Node Error [ERR_HTTP_HEADERS_SENT]
This post describes why the Node Error [ERR_HTTP_HEADERS_SENT] cannot set headers after they are sent.
Read more >
Node Error: Cannot set headers after they are sent to the client
Hey, I'm new to node and express and I'm getting this error: "Cannot set headers after they are sent to the client" everytime...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found