2

有没有办法检查使用 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)
4

3 回答 3

5

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

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

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

于 2014-08-25T13:53:07.507 回答
2

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

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

上面的行对我有用。

于 2013-04-18T20:52:06.893 回答
1
res.set("Content-disposition", "attachment; filename=\""+file.name+"\"")

这对我有用。

于 2016-08-12T21:31:53.780 回答