简体   繁体   English

如何测试响应是否已设置/发送?

[英]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) “发送”表示内容已发送(http 200)或http状态已设置为某事(例如,http状态204或重定向)

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. response.headersSent应该工作。

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!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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