Express.js - 如何检查头文件是否已经发送? [英] Express.js - How to check if headers have already sent?

查看:221
本文介绍了Express.js - 如何检查头文件是否已经发送?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个可能设置标题的库。如果头文件已经发送,我想给出一个自定义的错误信息,而不是仅仅让它失败,而Node.js给出的发送后不能设置头文件。那么如何检查头文件是否已经发送?

解决方案

简单:Connect的Response类提供了一个公共属性headerSent p>

res.headerSent 是一个布尔值,表示头文件是否已经发送到客户端。



从源代码:

  / ** 
*提供公共标头发送标志
*直到节点。
*
* @return {Boolean}
* @api public
* /

res .__ defineGetter __('headerSent',function(){
return this._header;
});

https://github.com/senchalabs/connect/blob/master/lib/patch.js#L22


I am writing a library which may set headers. I want to give a custom error message if headers have already sent, instead of just letting it fail with the "Can't set headers after they are sent" message given by Node.js. So how to check if headers have already sent?

解决方案

Simple: Connect's Response class provides a public property "headerSent".

res.headerSent is a boolean value that indicates whether the headers have already been sent to the client.

From the source code:

/**
   * Provide a public "header sent" flag
   * until node does.
   *
   * @return {Boolean}
   * @api public
   */

  res.__defineGetter__('headerSent', function(){
    return this._header;
  });

https://github.com/senchalabs/connect/blob/master/lib/patch.js#L22

这篇关于Express.js - 如何检查头文件是否已经发送?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆