stackers stackers - 15 days ago 4
Node.js Question

Send image generated with node-gd to client

I am trying to dynamically generate images using node-gd. My application is node.js/express.

This code successfully generates an image:

//create image
var gd = require('node-gd');
var img = gd.createSync(200, 80);
img.colorAllocate(0, 255, 0);

img.savePng('../test.png', 0, function(error) {
if (error) throw error;
img.destroy();
})


But it saves the image to a file. I don't necessarily want to keep the generated images, just send it to the client.

I was hoping I could just do

res.send(img);


But that just returns an object with metadata about the image.

Ben Ben
Answer

Would Express's res.sendFile method work for you?

res.sendFile('/path/to/image.png')