Node Express将图像文件作为API响应发送 [英] Node Express sending image files as API response

查看:161
本文介绍了Node Express将图像文件作为API响应发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我去谷歌,但找不到答案,但它一定是一个常见的问题。这与节点请求(读取图像流 - 管道)相同的问题返回回复),这是没有答复的。



如何将图像文件作为Express .send()响应发送?我需要将RESTful URL映射到图像 - 但是如何使用正确的标题发送二进制文件?例如,

 < img src ='/ report / 378334e22 / e33423222'/> 

通话...

  app.get('/ report /:chart_id /:user_id',function(req,res){
// authenticate user_id,get chart_id obfuscated url
// send image二进制与正确的标题
});


解决方案

Express中有一个api。

res.sendFile

  app.get('/ report /:chart_id /:user_id',function(req,res){
// res.sendFile(filepath);
});

http://expressjs.com/en/api.html#res.sendFile


I Googled this but couldn't find an answer but it must be a common problem. This is the same question as Node request (read image stream - pipe back to response), which is unanswered.

How do I send an image file as an Express .send() response? I need to map RESTful urls to images - but how do I send the binary file with the right headers? E.g.,

<img src='/report/378334e22/e33423222' />

Calls...

app.get('/report/:chart_id/:user_id', function (req, res) {
     //authenticate user_id, get chart_id obfuscated url
     //send image binary with correct headers
});

解决方案

There is an api in Express.

res.sendFile

app.get('/report/:chart_id/:user_id', function (req, res) {
    // res.sendFile(filepath);
});

http://expressjs.com/en/api.html#res.sendFile

这篇关于Node Express将图像文件作为API响应发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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