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?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With