express.static()是否在内存中缓存文件? [英] Does express.static() cache files in the memory?

查看:187
本文介绍了express.static()是否在内存中缓存文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ExpressJS for NodeJS中,我们可以执行以下操作:

In ExpressJS for NodeJS, we can do the following:

app.use(express.static(__dirname + '/public'));

提供所有静态CSS,JS和图像文件。我的问题是:

to serve all the static CSS, JS, and image files. My questions are these:

1)执行此操作时,Express是否会自动将文件缓存在服务器内存中,或者在每次出现以下情况之一时从硬盘读取文件?

1) When we do that, does Express automatically cache the files in the server's memory or does it read from the hard disk every time one of the resources is served?

2)执行此操作时,Express是否默认使用ETag将资源保存在客户端的硬盘上,还是仅保存在客户端的内存上?

2) When we do that, does Express, using ETag by default, save the resources on the client's hard disk, or on the client's memory only?

推荐答案


  1. 静态中间件不进行服务器端缓存。它可以让您执行两种客户端缓存方法:ETag和Max-Age:

如果浏览器看到 ETag 与页面一起使用,它将对其进行缓存。下次浏览器加载页面时,它将检查ETag编号是否更改。如果文件完全相同,则其ETag也完全相同-服务器以HTTP 304(未修改)状态代码作为响应,而不是再次发送所有字节并节省大量带宽。
Etag默认是打开的,但是您可以这样关闭它:

If the browser sees the ETag with the page, it will cache it. The next time the browser loads the page it checks for the ETag number changes. If the file is exactly the same, and so is its ETag - the server responds with an HTTP 304("not modified") status code instead of sending all the bytes again and saves a bunch of bandwidth. Etag is turned-on by default but you can turn it off like this:

app.use(express.static(myStaticPath, {
  etag: false
}))

max-age 会将最大时间设置为一定的时间,以便浏览器仅在一天过后才请求该资源。

Max-age is will set the max-age to some amount of time so the browser will only request that resource after one day has passed.

app.use(express.static(myStaticPath, {
  maxAge: '5000' // uses milliseconds per docs
}))

有关更多详细信息,请阅读此内容文章

For more details you can read this article


  1. 默认情况下,它位于硬盘驱动器上,但有人可以使用

  1. By default it's on the hard-drive, but someone can use something like this

这篇关于express.static()是否在内存中缓存文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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