ExpressJS / NodeJSredirect图像
img
可以将src值redirect到另一个页面?
鉴于,我有img
:
<img src='/images/fileName'/>
在app.js中
app.get('/images/:fileName', subject.image);
这是我的路线:
exports.image = function(req, res){ var fileName = req.fileName; fs.exists(fileName, function(exists){ if (exists) { res.sendfile(imagePath); }else{ res.redirect('http://gravatar.com/avatar/_some_hash'); } }); }
我的代码工作,但如果图像不存在,使用res.redirect
可以吗?
谢谢
是的,可以redirect。 最后,它只是一个GET的HTTP服务器和浏览器将遵循redirect。
正如在这个问题中提到的:( HTTPredirect图像是否可行? )您可能不想对很多图像做这样的事情。
你也可以设置默认图像:
<img src="/images/fileName" onerror="this.src='/images/default.png'" />