在 Express Node.js 中禁用 etag 标头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15191511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disable etag Header in Express Node.js
提问by Raj
We have a need to remove the etagheader from all HTTP responses in our Node.js Express application. We have a web services API written in Express where unexpected results are seen at the client when we send etags and the client sends back the if-none-matchheader.
我们需要etag从 Node.js Express 应用程序中的所有 HTTP 响应中删除标头。我们有一个用 Express 编写的 Web 服务 API,当我们发送 etags 并且客户端发回if-none-match标头时,客户端会看到意外的结果。
We've tried app.disable('etag')and res.removeHeader('etag'), but neither work; the app sends the header regardless.
我们试过app.disable('etag')and res.removeHeader('etag'),但都没有奏效;无论如何,应用程序都会发送标头。
Is there any other means of disabling this header in all responses?
是否有其他方法可以在所有响应中禁用此标头?
回答by alessioalex
app.disable('etag')should work now, there has been a pull request merged to deal with this:
app.disable('etag')现在应该可以工作了,已经合并了一个拉取请求来处理这个问题:
https://github.com/visionmedia/express/commit/610e172fcf9306bd5812bb2bae8904c23e0e8043
https://github.com/visionmedia/express/commit/610e172fcf9306bd5812bb2bae8904c23e0e8043
UPDATE: as Bigood pointed out in the comments the new way of doing things is the following
更新:正如 Bigood 在评论中指出的,新的做事方式如下
app.set('etag', false); // turn off
Change occured with version 3.9.0: https://github.com/strongloop/express/releases/tag/3.9.0
3.9.0 版发生了变化:https: //github.com/strongloop/express/releases/tag/3.9.0
For more options on setting the etag check the 4.x docs here: http://expressjs.com/4x/api.html#app.set
有关设置 etag 的更多选项,请在此处查看 4.x 文档:http: //expressjs.com/4x/api.html#app.set
回答by PapaKai
app.disable('etag')
This will disable etag header for all requests, but not for static contents. The following does it for static content:
这将禁用所有请求的 etag 标头,但不会禁用静态内容。以下对静态内容执行以下操作:
app.use(express.static(path.join(__dirname, 'public'), {
etag: false
}));
回答by Lukas Liesis
This sample works perfectly.
此示例完美运行。
- No undocumented hacks
- Fine tuning for each endpoint
Can add/remove any header :)
var onHeaders = require('on-headers') function myRoute(req, res) { scrubETag(res) // do the rest res.send('something') } function scrubETag(res) { onHeaders(res, function () { this.removeHeader('ETag') }) }
- 没有无证黑客
- 每个端点的微调
可以添加/删除任何标题:)
var onHeaders = require('on-headers') function myRoute(req, res) { scrubETag(res) // do the rest res.send('something') } function scrubETag(res) { onHeaders(res, function () { this.removeHeader('ETag') }) }
回答by loganfsmyth
It seems to me that the real solution to your problem would be to figure out why it is behaving strangely due to the etags.
在我看来,解决您的问题的真正方法是弄清楚为什么它会因 etags 出现奇怪的行为。
To answer your question though, Express does not currently have support for disabling the etags headers. It was actually discussed and merged in this pull request, but was later reverted. If you really need this and don't want to try to fix the root problem, you can always apply that patch and go from there.
不过,要回答您的问题,Express 目前不支持禁用 etags 标头。它实际上在这个 pull request 中被讨论和合并,但后来被恢复。如果您真的需要这个并且不想尝试解决根本问题,您可以随时应用该补丁并从那里开始。
回答by Tony
Looking at the express response.js, Etags are only sent when the request method is GET. You can prevent express from sending etags in the response by setting the request.method to something else before calling response.send().
查看express response.js,只有在请求方法为GET时才会发送Etags。您可以通过在调用 response.send() 之前将 request.method 设置为其他内容来防止 express 在响应中发送 etags。
eg:
例如:
app.get('/path/returnsJSON', function(req, res){
/* HACK to workaround the framework sending e-tags and "304 NOT MODIFIED" responses */
req.method="NONE"; // was "GET"
res.status(200).send({data:1});
});
This worked OK for me.
这对我有用。
回答by Kevin Danikowski
You Can't using express
你不能使用快递
You can only make them false, but that will still result in a 304 redirect because your etag will match the last request (which returned false)
您只能创建它们false,但这仍然会导致 304 重定向,因为您的 etag 将匹配最后一个请求(返回的请求false)
I used this solution from another npm package(https://github.com/helmetjs/nocache/issues/9) which uses on-headersnpm package to modify the header afterexpress adds the etag.
我使用了另一个 npm 包(https://github.com/helmetjs/nocache/issues/9)中的解决方案,它使用on-headersnpm 包在express 添加 etag后修改标头。
This can be added anywhere you have your resbeing called, for a specific route or all routes:
这可以添加到您res被呼叫的任何地方,用于特定路线或所有路线:
const onHeaders = require('on-headers');
onHeaders(res, function () {
this.removeHeader('etag');
});
Note, you may also need to change your cache settings. But this will remove your express etags :)
请注意,您可能还需要更改缓存设置。但这将删除您的快递 etags :)

