2013-03-22 97 views
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) { 
    ....... 
+1

如果'缓存Control'看起来很可疑:那些标题似乎是那些发送*到*服务器,没有收到* *从服务器。您的maxAge设置为1.8秒,这似乎相当低。这是它 – robertklep 2013-03-22 20:27:20

+0

@robertklep是的,在'maxAge'只是设置得太低,我想我想的不秒毫秒。我把它提升到了'3600000',现在它显示为'max-age = 3600'。谢谢!!顺便说一句,如果你在下面添加你的答案,我可以接受它。 – SkinnyG33k 2013-03-22 21:04:27

回答

4

maxAge是以毫秒为单位的值,而你的情况似乎相当低(1800,这是1.8秒)。这些资源可能会从缓存中到期之前,你甚至可以重新加载他们的机会,所以他们似乎永远不会缓存。

+0

缓存控制最大年龄为秒,不毫秒http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html – onlyurei 2015-01-08 20:34:39

+0

原来,而不是快速使用毫秒的静态中间件秒为单位对于最大年龄,令人困惑。 – onlyurei 2015-01-08 20:43:21

相关问题