【问题标题】:Is There a Way to Check Sent Headers with Node/ Express 2.x?有没有办法使用 Node/Express 2.x 检查发送的标头?
【发布时间】:2013-03-09 22:58:32
【问题描述】:

有没有办法检查使用 node/express 2.x 发送了哪些特定的标头?

我的文件下载在大多数情况下都能完美运行,但在某些特定情况下,我在 Chrome 中遇到错误(节点中没有错误):

Duplicate headers received from server
The response from the server contained duplicate headers. This problem is generally the result of a misconfigured website or proxy. Only the website or proxy administrator can fix this issue.
Error 349 (net::ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION): Multiple distinct Content-Disposition headers received. This is disallowed to protect against HTTP response splitting attacks.

出于测试目的,我想查看是否已发送特定标头,有没有办法使用 node.js 执行此操作?


...而且因为有人会问我用于设置标头的代码,所以我将流作为下载流,并且只在一个位置设置标头。

res.setHeader('Content-disposition', 'attachment; filename=' + filename)
res.setHeader('Content-Length', stats.size)
res.setHeader('Content-type', 'application/pdf')

stream.pipe(res)

【问题讨论】:

  • stream.pipe(res).on('end', function () {console.log(res.headers); }); 应该记录发送的标头,因为响应已结束。
  • 好主意!当流结束时,我已经有了一些事件处理程序。经过一番调查,我发现信息存储在res._headers你应该把它作为答案,我会接受它。

标签: node.js header stream express


【解决方案1】:
res.set("Content-disposition", "attachment; filename=\""+file.name+"\"")

这对我有用。

【讨论】:

    【解决方案2】:

    正如@generalhenry 在我的问题 cmets 中所说:

    stream.pipe(res).on('end', function () {
      console.log(res._headers); 
    });
    

    以上行对我有用。

    【讨论】:

      【解决方案3】:

      HTTP 响应是 WritableStream。当流关闭时,会发出 finish 事件。因此,听它就可以了:

      res.on('finish', function() {
          console.log(res._headers);
      });
      

      更加灵活。可以放在中间件或资源处理程序中。

      【讨论】:

        猜你喜欢
        • 2017-08-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-17
        相关资源
        最近更新 更多