Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node Express sending image files as API response

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 }); 
like image 845
metalaureate Avatar asked Jul 07 '13 19:07

metalaureate


People also ask

How do I send a picture to API?

Option 1: Direct File Upload , From this method you can select form-data and set the type to file. Then select an image file by clicking on the button shown in the value column. The content type is automatically detect by postman but if you want you can set it with a relevant MIME type.

How do I send a picture in node JS?

post('/upload', (req, res) => { // We'll handle the image upload here }); To handle file uploads we need the express-fileupload middleware, this will read the multipart/form-data and add a files property to the request object.


1 Answers

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

like image 129
Po-Ying Chen Avatar answered Sep 20 '22 10:09

Po-Ying Chen