问题描述
我正在使用Express 4提供图像流:
I am serving image stream using express 4 with :
app.get('/v1/images/:Uid/', function(req, res) {
var gcsbucket = gcs.bucket('bucket'),
remoteReadStream = gcsbucket.file(req.params.Uid+'.jpg').createReadStream();
remoteReadStream.pipe(res);
});
(图像存储在Google云存储中)
(the images are stored on Google cloud storage)
我正在尝试找到一种方法来告诉浏览器缓存我正在提供的图像.我尝试添加:
I am trying to find a way to tell browsers to cache the images I am serving.I tried to add :
res.set('Cache-Control', 'public, max-age=31557600');
但是当我在 http://server/v1/images/1234 ,它将始终重新加载图像.
But this is not changing anything when I access the API at http://server/v1/images/1234, it always reload the image.
使用流服务器端时告诉浏览器缓存响应的正确方法是什么?
What is the proper way to tell browsers to cache the response when using stream server-side ?
推荐答案
这实际上是有效的:
app.get('/v1/images/:Uid/', function(req, res) {
var gcsbucket = gcs.bucket('bucket'),
remoteReadStream = gcsbucket.file(req.params.Uid+'.jpg').createReadStream();
res.set('Cache-Control', 'public, max-age=31557600');
remoteReadStream.pipe(res);
});
奇怪的是,当您直接访问API时,浏览器永远不会缓存.但是,当您将图像嵌入到html页面(如:
Strangely, the browser never cache when you access directly the API. However the image is cached when you embed it in an html page like :
<img src="http://server/v1/images/1234">
对于我的用例来说已经足够了,但是如果有人有理由吗?
It is enough for my use case but if someone has the reason why ?
这篇关于快速缓存来自Google Cloud Storage的图像流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!