2

我花了一个多小时试图在生产中快速缓存静态文件。有什么我做错了吗?所有标头在第一个请求中返回 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) {
  .......
4

1 回答 1

4

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

于 2013-03-22T21:07:37.637 回答