大网数据广告活动
网站运营与故障问答

The Node Express server is returning "304 Not Modified" responses, but only after querying the database

· 52CE 我爱测

I'm just diving into web developent, my current passion is Express (Node.js). I've noticed then i often recieve HTTP/1.1 304 Not Modified either for static content and (!) for my json API requests (when data is still the same, so…

问题原文

原帖链接 · Diligent Key Presser · CC BY-SA 3.0

I'm just diving into web developent, my current passion is Express (Node.js).

I've noticed then i often recieve HTTP/1.1 304 Not Modified either for static content and (!) for my json API requests (when data is still the same, so this is actually correct). But my node console log shows that SQL querys are executed every time API requested.

It seems like requests from my front-end are cached, but the cache does not actually prevent my back-end from executing redundant SQL querys. Hmmm...

Can anyone shed some light onto what is actually going on here?

已采纳解答

原帖链接 · Stephen Ostermiller · CC BY-SA 3.0

The framework is:

  1. Generating the page internally for every request
  2. Using ETag headers with a content hash function to compare the generated page with the version sent before
  3. Either sending the full page, or a "Not Modified" response based on the hash comparison

Because your content is database generated, the framework has to assume that it is dynamic and can change with every request. Therefore it must execute all the queries and generate the page before it compare to the version the client actually has.

I'm not familiar with Express, but most frameworks have a mechanism where you can cache the results of SQL queries for some amount of time to reduce the load on the database.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2016-09-08)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。