如何使用Express.js返回304未修改状态? [英] How do I return 304 Unmodified status with Express.js?

查看:324
本文介绍了如何使用Express.js返回304未修改状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用node并表示iOS应用程序的后端。数据存储在SQL Server数据库中,因此iOS应用程序查询服务器,服务器查询数据库,服务器接收db响应,然后将响应转发给iOS应用程序。我试图弄清楚缓存的工作方式。我提供了大量静态内容-例如博客文章。因此,我计划使用etags,但是我不确定它应该如何工作。我发出请求,获取内容,并将响应缓存在客户端。好。然后,我稍后会使用存储在 If-None-Match标题中的上一个响应的etag发出相同的请求。那呢

I'm using node and express for the back end of an iOS application. Data is stored in a SQL Server database, so iOS apps query the server, the server queries the database, server receives the db response, and then forwards the response to the iOS application. I'm trying to figure out how caching works though. I'm serving a lot of static content - blog articles for example. So I planned to use etags, but I'm not sure how it's supposed to work. I make a request, get content, and cache the response on the client side. Ok. Then I make the same request later with the etag of the previous response stored in the 'If-None-Match' header. Then what?

Express.js是否可以自动处理?似乎没有-我无法获得它来生成304响应。如果尝试在发送响应之前检查响应标头,则会得到null,因此在发送响应之前无法获取响应的etag。那么我应该如何比较请求etag和服务器将发回的内容的etag?我应该使用自定义生成的etag并将其缓存在服务器上,然后将请求etag与该缓存进行比较吗?

Does Express.js handle this automatically? It doesn't seem to - I couldn't get it to generate a 304 response. If I try to check the response headers before I send the response, I get null, so I can't get the etag of a response before I send it. So how am I supposed to compare the request etag with the etag of the content that the server would send back? Am I supposed to use custom generated etags and cache these on the server, then compare request etags with this cache?

下面是我设置的一个非常简单的路由来测试,不涉及数据库。我只发送一个数字到服务器,它返回平方。如果我将带有etag的请求发送到相同的网址,则会得到相同的响应。我可以检查请求的 If-None-Match标头,但是要与我进行比较以确定是否应该发送304而不是200状态?

Below is a very simple route I set up to test this, no database involved. I just send a number to the server, and it returns the square. If I send a request with an etag to the same url, I will get the same response. I can check the 'If-None-Match' header of the request, but to what do I compare it in order to determine if I should send a 304 instead of a 200 status?

router.use("/square/:testId", function(req, res) {

  var obj = {};
  obj["testId"] = req.params.testId;
  obj["result"] = req.params.testId * req.params.testId;

  res.setHeader('Cache-Control', 'public, max-age=5');

  var h2 = JSON.stringify(res.headers,null,2);
  console.log("The response headers: " + h2);
  //Prints null

  res.status(200).send(obj);
});


推荐答案


我应该使用自定义生成的etag并将其缓存在服务器上,然后将请求etag与该缓存进行比较?

Am I supposed to use custom generated etags and cache these on the server, then compare request etags with this cache?

是的,您可以使用类似 etag npm模块,该模块可以使用Buffer或字符串并为您提供强大的etag值: res.setHeader('ETag',etag(body))。如果要将修改日期存储在数据库中,则还可以简单地将它们以 Last-Modified 标头的形式发送给客户端。在那种情况下,客户端最终将在后续请求中发送 If-Modified-Since ,然后您可以将其用于与请求资源的修改列进行比较。

Yes, you can use something like the etag npm module which can take a Buffer or string and provide you with a strong etag value: res.setHeader('ETag', etag(body)). If you are storing modification dates in your database then you could also simply send those down to the client in the form of a Last-Modified header. In that case the client would end up sending an If-Modified-Since in subsequent requests which you could then use to compare to the modification column of the resource being requested.

无论哪种情况,您都将负责使用相应的状态码,标题和正文进行响应。

In either case you will be responsible for responding with the appropriate status code, headers, and body.

如果从实际的文件系统提供静态文件,那么您只需使用 serve-static 表达中间件,它将自动提供正确的缓存行为。

If you're serving static files from the actual file system then you could just use the serve-static express middleware which will automatically provide the correct caching behavior.

这篇关于如何使用Express.js返回304未修改状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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