简体   繁体   English

从子域 Express.js 发送图像

[英]Send Image from subdomain Express.js

Lets say I have this for local testing.可以说我有这个用于本地测试。

sendImage: async function(req, res) {

    console.log(req.hostname);

    var filepath = path.join(__dirname, '../../img/uploads/' + req.params.year + '/' + req.params.month + '/' + req.params.id);
    //console.log(filepath);
    res.sendFile(filepath);
}

This gets a file based on a path to the file of course.这当然会根据文件的路径获取文件。 Now lets say that the img folder is actually the folder to the subdomain, and I wanted to send the image from the subdomain itself, so instead of the api requesting to the main site aka mysite.com, it would request from img.mysite.com.现在假设 img 文件夹实际上是子域的文件夹,我想从子域本身发送图像,所以不是 api 请求主站点 aka mysite.com,而是从 img.mysite 请求。 com。 I have this set up in such a way that it goes from img={'/api/date/img}我的设置方式是从 img={'/api/date/img}

Basically I need the image requests to go from mysite.com/api/date/img to img.mysite.com/date/img基本上我需要从 mysite.com/api/date/img 到 img.mysite.com/date/img 的 go 的图像请求

Is there a way to do this with express directly?有没有办法直接用快递做到这一点?

By the time you are in the middle of this code, the browser has already requested the image from the Express server.当您处于这段代码的中间时,浏览器已经从 Express 服务器请求了图像。 It's too late to stop it from requesting this URL.现在阻止它请求这个 URL 为时已晚。

So your options:所以你的选择:

  • Have the express server give the browser the image it requested (which is what you are doing now)让快递服务器向浏览器提供它请求的图像(这就是你现在正在做的)
  • Issue a redirect response to tell the browser that the image isn't at the URL and it should go to a different URL (on the subdomain)发出重定向响应以告诉浏览器该图像不在 URL 中,它应该 go 到不同的 URL(在子域上)
  • Take a step back and change the code which told the browser to ask for this URL so it outputs the URL on the subdomain instead.退后一步,更改告诉浏览器请求此 URL 的代码,以便它在子域上输出 URL。

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

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