Html ExpressJS/NodeJS重定向映像
Html ExpressJS/NodeJS重定向映像,html,node.js,redirect,express,Html,Node.js,Redirect,Express,img可以将src值重定向到另一个页面吗 鉴于此,我有img: <img src='/images/fileName'/> <img src="/images/fileName" onerror="this.src='/images/default.png'" /> 这是我的路线: exports.image = function(req, res){ var fileName = req.fileName; fs.exists(fileName, f
img
可以将src值重定向到另一个页面吗
鉴于此,我有img
:
<img src='/images/fileName'/>
<img src="/images/fileName" onerror="this.src='/images/default.png'" />
这是我的路线:
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
谢谢是的,可以重定向。最后,只需访问HTTP服务器,浏览器就会跟随重定向
如本问题中所述:()对于许多图像,您可能不希望这样做。您也可以设置默认图像:
<img src="/images/fileName" onerror="this.src='/images/default.png'" />