简体   繁体   中英

How do i test if the response was already set/send?

I have the case that a express controller action "may" send contents.

"Send" means either content was send (http 200) or the http status was set to something (http status 204 or a redirect for example)

If nothing was sent/set a default routine should send a default content.

how can i test in my default routine if the express controller action already set contents or set the status code ?

response.headersSent should work.

For example:

if (response.headersSent) {
    console.log('Headers sent!')
} else {
    console.log('Headers have not been sent.')
}
res.writeHead(200);
if (response.headersSent) {
    console.log('Headers sent!')
} else {
    console.log('Headers have not been sent.')
}

Connecting with a client should log:

Headers have not been sent.
Headers sent!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM