简体   繁体   English

Express.js 的自定义计算 Etag

[英]Custom Computed Etag for Express.js

I'm working on a simple local image server that provides images to a web application with some JSON.我正在开发一个简单的本地图像服务器,该服务器使用一些 JSON 向 Web 应用程序提供图像。 The web application has pagination that will do a get request "/images?page=X&limit&200" to an express.js server that returns the JSON files in a single array. Web 应用程序具有分页功能,它将向 express.js 服务器发出获取请求“/images?page=X&limit&200”,该服务器在单个数组中返回 JSON 文件。 I want to take advantage of the browser's internal caching such that if a user goes to a previous page the express.js returns an ETAG.我想利用浏览器的内部缓存,这样如果用户转到上一页,express.js 会返回一个 ETAG。 I was wondering how this could be achieved with express.js?我想知道如何使用 express.js 实现这一点? For this application, I really just want the computation of the ETAG to take in three parameters the page, the directory, and the limit (It doesn't need to consider the whole JSON body).对于这个应用程序,我真的只希望 ETAG 的计算采用页面、目录和限制三个参数(不需要考虑整个 JSON 主体)。 Also this application is for local use only, so I want the server to do the heavy lifting since I figured it be faster than the browser.此外,此应用程序仅供本地使用,因此我希望服务器完成繁重的工作,因为我认为它比浏览器更快。 I did see https://www.npmjs.com/package/etag which seems promising, but I'm not sure how to use it with express.js我确实看到了似乎很有希望的https://www.npmjs.com/package/etag ,但我不确定如何将它与 express.js 一起使用

Here's a boilerplate of the express.js code I have below:这是我在下面的 express.js 代码的样板:

 var express = require('express'); var app = express(); var fs = require('fs'); app.get('/', async (req, res) =>{ let files = []; let directory = fs.readdirSync("mypath"); let page = parseInt(req.query.page); let limit = parseInt(req.query.limit); for (let i = 0; i < limit; ++i) { files.push(new Promise((resolve) => { fs.readFile(files[i + page * limit].name, (err, data) => { // format the data so easy to use for UI resolve(JSON.parse(data)); }); }); } let results = await Promise.all(files); // compute an etag here and attach it the results. res.send(results); }); app.listen(3000);

When your server sends an ETag to the client, it must also be prepared to check the ETag that the client sends back to the server in the If-None-Match header in a subsequent "conditional" request.当您的服务器向客户端发送 ETag 时,它还必须准备检查客户端在后续“条件”请求中的If-None-Match标头中发送回服务器的 ETag。

If it matches, the server shall respond with status 304 ;如果匹配,服务器将响应状态 304 otherwise there is no benefit in using ETags.否则使用 E​​Tag 没有任何好处。

var serverEtag = "<compute from page, directory and limit>";
var clientEtag = req.get("If-None-Match");
if (clientEtag === serverEtag) res.status(304).end();
else {
  // Your code from above
  res.set("ETag", serverEtag);
  res.send(results);
}

The computation of the serverEtag could be based on the time of the last modification in the directory, so that it changes whenever any of the images in that directory changes. serverEtag的计算可以基于目录中最后一次修改的时间,因此只要该目录中的任何图像更改,它就会更改。 Importantly, this could be done without carrying out the fs.readFile statements from your code.重要的是,这可以在不执行代码中的fs.readFile语句的情况下完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM