Send image file as an API response in Node.js

Today we will show you how to send image file as API response in Node.js with Express.js framework. You may need to report an image file or a PDF file as an API response so you can use the following functions to send the file directly.

Way to send image file as an API response in Node.js

  1. Using res.sendFile() function
  2. Using res.send() function

1. Using res.sendFile() function

Use the res.sendFile() function to transfers the file at the given path. Sets the Content-Type response HTTP header field based on the filename’s extension. Unless the root option is set in the options object, path must be an absolute path to the file.

2. Using res.send() function

In the second solution, we can use the res.send() function to send file by passing HTML content.

I hope you find this article helpful.
Thank you for reading. Happy Coding..!! 🙂

If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

2 Responses

  1. Daviddag says:

    Thanks, I’ve been looking for this for a long time

Leave a Reply

Your email address will not be published. Required fields are marked *