ExpressJS / NodeJS重定向图像 [英] ExpressJS / NodeJS Redirect Image

查看:135
本文介绍了ExpressJS / NodeJS重定向图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于 img 来说,将src值重定向到另一个页面可以吗?

Is it okay for an img to have src value with redirect to another page?

我有 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 if image does not exist?

My code works, but is it okay to use res.redirect if image does not exist?

谢谢

Thanks

推荐答案

是的,可以重定向。最后,它只是一个HTTP到HTTP服务器和浏览器将遵循重定向。

Yes, it's ok to redirect. In the end its just a GET to the HTTP server and the browser will follow the redirect.

正如在这个问题中提到的:( HTTP可以重定向图片吗?)你可能不想对很多图片做这件事。

As mentioned in this question: (Is it OK to HTTP redirect images?) you might not want do to that for lots of images.

这篇关于ExpressJS / NodeJS重定向图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆