如何让nodejs服务器充当代理并从cloudinary获取img然后将其发送到浏览器

发布于 2025-01-15 05:53:46 字数 791 浏览 3 评论 0原文

对于存储空间问题,我无法将图像保存在服务器中,因此我必须将其存储在 cloudinary 中 出于搜索引擎优化的目的,我必须从我的域而不是 cloudinary 的域中提供它 所以我想从cloudinary获取img文件并将其直接发送到浏览器(从我的域名提供服务) 我缺少的是将我从 cloudinary api 获得的 img 文件转换为正确的形式,以便我可以使用 nodejs 中的响应对象发送它 代码

app.get('/uploads/img/:imgName', (req, res) => {
    axios.get('https://res.cloudinary.com/dkhccaa25/image/upload/blog_img/${req.params.imgName}')
    .then(response => {
    console.log(response);
    /* how to convert response into the right format so it can be sent */
    //
    //
    //
})
.then (response => {
    /*converted response */
    res.sendFile(response)

  })
  .catch(error => {
    console.log(error);
  });

这是我如何能够将文件从节点服务器发送到浏览器的 ,以便可以使用显示它

for storage space issues i cannot save images in server so i had to store it in cloudinary
and for seo purposes I had to serve it from my domain not cloudinary's
so i thought to get img files from cloudinary and send it directly to browser (to be served from my domain name )
what i am missing is converting the img file i got from cloudinary api into the right form so i can send it using response object in nodejs
here is the code

app.get('/uploads/img/:imgName', (req, res) => {
    axios.get('https://res.cloudinary.com/dkhccaa25/image/upload/blog_img/${req.params.imgName}')
    .then(response => {
    console.log(response);
    /* how to convert response into the right format so it can be sent */
    //
    //
    //
})
.then (response => {
    /*converted response */
    res.sendFile(response)

  })
  .catch(error => {
    console.log(error);
  });

how I can be able to send the file from node server to browser so it can be displayed using
<img src="img url...">

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

自由范儿 2025-01-22 05:53:47

您不必使用 res.sendFile,这需要将其保存到文件系统。基本上 - 接受响应并直接将其与上游响应发送到客户端的正确内容类型标头一起传递。

最小的例子:


const express = require('express');
const axios = require('axios');
const app = express();

app.get('/', (req, res) => {
  axios.get('https://static.pexels.com/photos/45201/kitty-cat-kitten-pet-45201.jpeg').then((axiosResp) => {
    res.header('content-type', axiosResp.headers['content-type']).send(axiosResp.data);
  });
});

app.listen(3000);

You do not have to use res.sendFile, this will require saving it to the filesystem. Basically - accept the response and pass it directly with the correct content-type header send by the upstream response to the client.

Minimal example:


const express = require('express');
const axios = require('axios');
const app = express();

app.get('/', (req, res) => {
  axios.get('https://static.pexels.com/photos/45201/kitty-cat-kitten-pet-45201.jpeg').then((axiosResp) => {
    res.header('content-type', axiosResp.headers['content-type']).send(axiosResp.data);
  });
});

app.listen(3000);
不语却知心 2025-01-22 05:53:47

最后我找到了这个解决方案。

const express = require('express');
const axios = require('axios');
const app = express();

app.get('/media', (req, res) => {
  axios({
    method: 'get',
    url: req.query.media,
    responseType: 'arraybuffer'
  })
  .then((result) => {
     res
       .header("content-type", result.headers['content-type'])
       .send(Buffer.from(result.data, 'binary')
  });
});

app.listen(3000);

Finally I found this solution.

const express = require('express');
const axios = require('axios');
const app = express();

app.get('/media', (req, res) => {
  axios({
    method: 'get',
    url: req.query.media,
    responseType: 'arraybuffer'
  })
  .then((result) => {
     res
       .header("content-type", result.headers['content-type'])
       .send(Buffer.from(result.data, 'binary')
  });
});

app.listen(3000);

提笔书几行 2025-01-22 05:53:47

最后通过编辑 @madflow 答案解决了问题(感谢他)

const express = require('express');
const axios = require('axios');
const app = express();

app.get('/', (req, res) => {
  axios.get('https://static.pexels.com/photos/45201/kitty-cat-kitten-pet-45201.jpeg', {responseType: 'stream'})
.then((axiosResp) => {
     res.set({
       'Content-Type': axiosResp.headers['content-type']
     })
     axiosResp.data.pipe(res)

  });
});

app.listen(3000);

finally the problem solved by editing on @madflow answer (thanks for him )

const express = require('express');
const axios = require('axios');
const app = express();

app.get('/', (req, res) => {
  axios.get('https://static.pexels.com/photos/45201/kitty-cat-kitten-pet-45201.jpeg', {responseType: 'stream'})
.then((axiosResp) => {
     res.set({
       'Content-Type': axiosResp.headers['content-type']
     })
     axiosResp.data.pipe(res)

  });
});

app.listen(3000);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文