img
可以具有 src 值并重定向到另一个页面吗?
在 View 中,我有 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
吗?
谢谢
最佳答案
是的,可以重定向。最后,它只是对 HTTP 服务器的 GET,浏览器将遵循重定向。
正如这个问题中提到的:( Is it OK to HTTP redirect images? )您可能不希望对大量图像这样做。
关于html - ExpressJS/NodeJS 重定向图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17966255/