Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 441
topskip Avatar asked Jan 14 '12 15:01

topskip


People also ask

What is scaffolding in Express JS?

Scaffolding is creating the skeleton structure of application. It allows users to create own public directories, routes, views etc. Once the structure for app is built, user can start building it. Express is the open source web development framework for Node. js for building web applications and the APIs.

What is bin www in Express?

Remember that bin/www is a script used to launch the application. It is written as a CommonJS script, but because app. mjs is now an ES6 module, bin/www also must be rewritten as an ES6 module. A CommonJS module cannot, at the time of writing, import an ES6 module. Change the filename: $ mv bin/www bin/www.mjs.


1 Answers

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.

like image 143
Ryan Olds Avatar answered Nov 15 '22 08:11

Ryan Olds