express.js的自定义计算的ETAG
我正在使用一个简单的本地图像服务器,该服务器将图像提供带有一些JSON的Web应用程序。 Web应用程序具有分页,可以执行get request“/images?page = x& limit& amp; 200” to Express.js服务器,该服务器将返回单个数组中的JSON文件。我想利用浏览器的内部缓存,以便如果用户转到上一页,则express.js返回ETAG。我想知道express.js如何实现这一目标?对于此应用程序,我真的只希望ETAG的计算在页面,目录和限制的三个参数中(它不需要考虑整个JSON主体)。此外,此应用程序仅用于本地使用,因此我希望服务器进行繁重的工作,因为我认为它比浏览器快。我确实看到 https://www.npmjs.com/package/package/etag 似乎有希望的,但是我不确定如何与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);
I'm working on a simple local image server that provides images to a web application with some JSON. 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. 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. I was wondering how this could be achieved with 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). 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
Here's a boilerplate of the express.js code I have below:
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您的服务器将ETAG发送给客户端时,还必须准备检查客户端在
if-none-match
标题在后续的“条件”请求中。如果匹配,服务器应响应状态304 ;否则,使用ETAG将没有任何好处。
serveretag
的计算可以基于目录中最后一个修改的时间,以便每当目录中的任何图像更改时会更改。重要的是,这可以在不执行代码中的fs.ReadFile
语句的情况下完成。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.If it matches, the server shall respond with status 304; otherwise there is no benefit in using ETags.
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. Importantly, this could be done without carrying out thefs.readFile
statements from your code.