【问题标题】:Express isn't setting max-age headersExpress 没有设置 max-age 标头
【发布时间】:2013-03-12 18:02:16
【问题描述】:

我已经花了一个多小时试图在生产环境中快速缓存静态文件。有什么我做错了吗?所有标头在第一个请求中返回 200,在后续请求中返回 304。我什至尝试将代码粘贴到主 app.configure 中,并直接从 express 文档中粘贴代码。

Request URL:http://localhost:3000/javascripts/jquery.min.js
Request Method:GET
Status Code:304 Not Modified
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Cache-Control:max-age=0


// Generated by CoffeeScript 1.3.3
(function() {
  var app, express, fs, http, path;

  express = require('express');
  http = require('http');
  path = require('path');
  fs = require('fs');

  app = express();

  app.configure(function() {
    app.set('port', process.env.PORT || 3000);
    app.set('views', __dirname + '/views');
    app.set('view engine', 'jade');
    app.use(express.favicon());
    app.use(express.logger('dev'));
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(express.compress());
    return app.use(require('less-middleware')({
      src: __dirname + '/public'
    }));
  });

  app.configure('development', function() {
    app.use(express["static"](__dirname + '/public'));
    app.use(app.router);
    app.use(express.errorHandler());
    return console.log("Hello from dev");
  });

  app.configure('production', function() {
    app.use(express["static"](__dirname + '/public', {maxAge: 1800}));
    app.use(app.router);
    return console.log("Hello from prod");
  });

  app.get('/', function(req, res) {
  .......

【问题讨论】:

  • 如果Cache-Control 看起来可疑:这些标头似乎是发送服务器的,而不是服务器接收的。您的 maxAge 设置为 1.8s,这似乎相当低。
  • @robertklep 是的,maxAge 设置得太低了,我想我想的是秒而不是毫秒。我将它提高到3600000,现在它显示为max-age=3600。谢谢!!顺便说一句,如果您在下面添加答案,我可以接受。

标签: node.js caching express


【解决方案1】:

maxAge 是以毫秒为单位的值,在您的情况下似乎很低(1800,即 1.8 秒)。资源可能在您有机会重新加载它们之前就从缓存中过期,因此它们似乎永远不会被缓存。

【讨论】:

猜你喜欢
  • 2020-12-16
  • 2023-03-12
  • 2012-03-04
  • 1970-01-01
  • 2020-09-06
  • 1970-01-01
  • 2020-08-11
  • 1970-01-01
  • 2017-07-16
相关资源
最近更新 更多