topskip
topskip

Reputation: 17335

express.js / serve generated image

I am using express and I would like to serve a generated image. The html source is something like <img src="/generated/image"> and the route setup so when a GET to /generated/image is called, a PNG gets created (and put, for example, in the public directory, but I guess this is not strictly necessary). Now I'd like to send back that image to the user. Should I use res.send() for that purpose? How can I get the data to the user?

Upvotes: 6

Views: 7732

Answers (1)

Ryan Olds
Ryan Olds

Reputation: 4847

Depending on library you're using to create the image, you should be able to res.end(image, 'binary'); Make sure to include the correct Content-Type header.

If you don't want to generate the file every time you can write the file to disk, store in a key/value store, relational database, etc... and check/serve if it has already been created before going through the creation routines.

Upvotes: 10

Related Questions