node.js 如何在 Connect/Express 静态中间件中完全阻止 HTTP 304 响应?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14641308/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:04:34  来源:igfitidea点击:

How to totally prevent HTTP 304 responses in Connect/Express static middleware?

node.jsnode.js-connect

提问by Jacob Marble

At times during development, it would be really niceto prevent HTTP 304 responses (in favor of 200's), and cause the Connect/Express static middlewareto read every response from the filesystem, rather than do any caching at all.

有时在开发过程中,阻止 HTTP 304 响应(支持 200 响应)并导致Connect/Express 静态中间件从文件系统读取每个响应,而不是进行任何缓存,这真的很好

I have tried playing with maxAgevalues of 0 and 1, to no avail:

我尝试使用maxAge0 和 1 的值,但无济于事:

app.use(express.static(__dirname + '/public', { maxAge: 1 }))

回答by Mark Selby

I get 200 responses by doing this during development :

通过在开发过程中这样做,我得到了 200 个响应:

var express = require('express');
app = express();
app.use(function(req, res, next) {
  req.headers['if-none-match'] = 'no-match-for-this';
  next();    
});

回答by Mohan R

app.disable('etag');

preventing 'etag' in response may help

防止“etag”作为回应可能会有所帮助

回答by Jonathan Ong

it does read from the file system on every response. it's just that if the request ETAG matches the response ETAG, it doesn't send the body of the response because it doesn't have to . It's the same file with the same hash. this is how 304 responses work.

它确实在每次响应时从文件系统中读取。只是如果请求 ETAG 与响应 ETAG 匹配,它不会发送响应正文,因为它不必。这是具有相同散列的同一个文件。这就是 304 响应的工作方式。

why do you want to prevent 304 responses?

为什么要阻止 304 响应?

回答by Jacob Marble

This solution is just a workaround. You could solve the problem from the browser side by disabling caching in Chrome. This doesn't help you if you need to work on something outside of Chrome, like Safari on iOS.

此解决方案只是一种解决方法。您可以通过在 Chrome 中禁用缓存来从浏览器端解决问题。如果您需要在 Chrome 之外的东西上工作,比如 iOS 上的 Safari,这对您没有帮助。