浏览器缓存机制,其实主要就是HTTP协议定义的缓存机制(如: Expires; Cache-control等)
其中针对于cache-control在express中是这样描述的:
express.static(root, [options])
express.static
是 Express 内置的唯一一个中间件。是基于 serve-static 开发的,负责托管 Express 应用内的静态资源。
root
参数指的是静态资源文件所在的根目录。
options
对象是可选的,支持以下属性:
属性 | 描述 | 类型 | 默认值 |
---|
dotfiles | Option
for serving dotfiles. Possible values are “allow”, “deny”, and “ignore” | String | “ignore” |
etag | Enable or disable etag generation | Boolean | true |
extensions | Sets
file extension fallbacks. | Boolean | false |
index | Sends directory index file. Set false to disable directory
indexing. | Mixed | “index.html” |
lastModified | Set the Last-Modified header to the last modified date of the file on the OS. Possible
values are true orfalse . | Boolean | true |
maxAge | Set the max-age property of the Cache-Control
header in milliseconds or a string in ms format | Number | 0 |
redirect | Redirect to
trailing “/” when the pathname is a directory. | Boolean | true |
setHeaders | Function for setting HTTP headers to serve with the file. | Function | |
以上方法中的option:
使用方法如下:
var express = require('express');
var path = require("path");
var app = express();
var options = {
maxAge: 3600000
};
app.use(express.static(path.join(__dirname, "./"), options));
app.listen(3000);