JS Errors: Resolving HTTP Headers sent error in Node.JS

When using express and Node.JS, we will sometimes get this error:

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at new NodeError (node:internal/errors:277:15)
at ServerResponse.setHeader (node:_http_outg…


This content originally appeared on DEV Community and was authored by Johnny Simpson

When using express and Node.JS, we will sometimes get this error:

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at new NodeError (node:internal/errors:277:15)
    at ServerResponse.setHeader (node:_http_outgoing:563:11)
    at ServerResponse.header (/node_modules/express/lib/response.js:771:10)
    at file:///link/to/file/app.js:309:13 {
    code: 'ERR_HTTP_HEADERS_SENT'
}

This is quite a confusing error if you aren't familiar with HTTP headers. This error arises when you send more than 1 responses to the user or client. That means the receiver is getting two responses, when it should only be getting one. To solve this, make sure you are only sending one response.

How to solve the ERR_HTTP_HEADERS_SENT error

This can often be caused when you send a response to the client, and an asychronous piece of code then sends a second response after the first. Look in your code, you may be accidentally using res.send twice. For example, the below will cause the error:

app.get('/app', async function(req, res) {
    /* Don't do this! Remove one of the res.send functions to remove the error */
    await User.find({username: req.headers.username}, function(err, items) {
        res.send('User');
    })
    res.send('Hello');
})

Note: other res functions, such as res.redirect will cause the same issue, i.e. the below code is also wrong:

app.get('/app', function(req, res) {
    /* Don't do this! Remove one of these functions to remove the error */    
    await User.find({username: req.headers.username}, function(err, items) {
        res.redirect('/app/login');    
    })
    res.send('Hello');
})

Your code should instead look like this, with only one res function:

app.get('/app', function(req, res) {
    res.redirect('/app/login');
})


This content originally appeared on DEV Community and was authored by Johnny Simpson


Print Share Comment Cite Upload Translate Updates
APA

Johnny Simpson | Sciencx (2022-02-15T12:13:49+00:00) JS Errors: Resolving HTTP Headers sent error in Node.JS. Retrieved from https://www.scien.cx/2022/02/15/js-errors-resolving-http-headers-sent-error-in-node-js/

MLA
" » JS Errors: Resolving HTTP Headers sent error in Node.JS." Johnny Simpson | Sciencx - Tuesday February 15, 2022, https://www.scien.cx/2022/02/15/js-errors-resolving-http-headers-sent-error-in-node-js/
HARVARD
Johnny Simpson | Sciencx Tuesday February 15, 2022 » JS Errors: Resolving HTTP Headers sent error in Node.JS., viewed ,<https://www.scien.cx/2022/02/15/js-errors-resolving-http-headers-sent-error-in-node-js/>
VANCOUVER
Johnny Simpson | Sciencx - » JS Errors: Resolving HTTP Headers sent error in Node.JS. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/02/15/js-errors-resolving-http-headers-sent-error-in-node-js/
CHICAGO
" » JS Errors: Resolving HTTP Headers sent error in Node.JS." Johnny Simpson | Sciencx - Accessed . https://www.scien.cx/2022/02/15/js-errors-resolving-http-headers-sent-error-in-node-js/
IEEE
" » JS Errors: Resolving HTTP Headers sent error in Node.JS." Johnny Simpson | Sciencx [Online]. Available: https://www.scien.cx/2022/02/15/js-errors-resolving-http-headers-sent-error-in-node-js/. [Accessed: ]
rf:citation
» JS Errors: Resolving HTTP Headers sent error in Node.JS | Johnny Simpson | Sciencx | https://www.scien.cx/2022/02/15/js-errors-resolving-http-headers-sent-error-in-node-js/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.