Error: Can't set headers after they are sent. (Express 4)
See original GitHub issueI 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:
- Created 8 years ago
- Comments:18 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
because you might send no less than 2 times response.
like my code:
I used
Promise.then()
and my functionnext()
goes before myres.send()
, so I had response more than 1 times to front-end. like me, you can write like this.