简体   繁体   English

用外部URL响应请求

[英]Respond to request with external URL

I'd like to respond to a particular request in different ways based on the referrer. 我想根据引荐来源网址以不同的方式响应特定的请求。 The code is pretty simple: 代码很简单:

app.get('/test', function(req, res) {        
    if (req.headers.accept.indexOf('image') != -1) {
        res.sendfile('http://example.com/someimage.png');
    } else {
        res.render('views/index');
    }
});

Basically, in the first conditional, I'd just like to 'pass long' an image from an external URL. 基本上,在第一个条件中,我只想“长传”来自外部URL的图像。 I assume I may have to stream this file through my server, but is there another way to just pass the link directly? 我假设我可能必须通过服务器流式传输此文件,但是还有另一种直接传递链接的方法吗?

Yes, assuming you use express as it seems, you can use redirect : 是的,假设您似乎使用express,则可以使用redirect

res.redirect('http://example.com/someimage.png');

If you want the browser to consider the new URL as permanent, add the relevant status code : 如果您希望浏览器将新的URL视为永久URL,请添加相关的状态代码:

res.redirect(301, 'http://example.com/someimage.png');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM