如何鼓励客户端的浏览器使用node.js中缓存中的图像?

时间:2015-08-04 15:49:11

标签: node.js caching amazon-s3

在我的应用中,只要将新图像上传到服务器,它就会获得唯一的名称。 这意味着如果客户端下载了一个文件,只要它存在于服务器上,它就会保持不变。

如果客户端目前在其缓存文件夹中有图像,则客户端无需重新下载该图像。

如何强制/鼓励浏览器保留缓存版本,而不是让他每次都重新下载图像?

我不知道它是否相关,但是当客户端请求文件时,它会从Amazon S3流式传输给他:

var AWS = require('aws-sdk');
module.exports = function (router) {
    router.get('/*', function (req,res) {
        var path = req.url
        while(path.charAt(0) === '/')
            path = path.substr(1);
        var s3 = new AWS.S3();
        var params = {Bucket: 'mybucket', Key: path};
        s3.getObject(params).createReadStream().pipe(res)
    })
}

1 个答案:

答案 0 :(得分:0)

在传递给res之前,请设置适当的缓存标头:

================================================================================
Error executing action `converge` on resource 'machine[Centos]'
================================================================================

RuntimeError
------------
Error: command 'chef-client -l info' exited with code 1.

Resource Declaration:
---------------------
# In C:/Users/xyz/.chef/local-mode-cache/cache/cookbooks/vmware-machines/recipes/machines.rb

 36: machine "Centos" do
 37:    #run_list ['vmware::machines']
 38: end

Compiled Resource:
------------------
# Declared in C:/Users/xyz/.chef/local-mode-cache/cache/cookbooks/vmware-machines/recipes/machines.rb:36:in `from_file'

machine("Centos") do
  action :converge
  retries 0
  retry_delay 2
  default_guard_interpreter :default
  chef_server {:chef_server_url=>"chefzero://localhost:8889", :options=>{:client_name=>"chef-local", :signing_key_filename=>"C:\\eclipse\\xyz\\workspace\\xyz\\vmware\\target\\.chef\\dummy.pem"}}
  driver "vsphere://vcenter-01.xyz.xyz.com/sdk?use_ssl=true&insecure=true"
  machine_options {:bootstrap_options=>{:use_linked_clone=>true, :num_cpus=>2, :memory_mb=>4096, :network_name=>["VM Network"], :datacenter=>"Sandbox", :datastore=>"Datastore_Esx2", :resource_pool=>"Box-Cluster", :template_name=>"Template", :template_folder=>"Templates/", :customization_spec=>{:ipsettings=>{:dnsServerList=>["xyz"]}, :domain=>"xyz"}, :ssh=>{:user=>"root", :password=>"password", :paranoid=>false}}}
  declared_type :machine
  cookbook_name "vmware-machines"
  recipe_name "machines"
end