I'm developping a kind of API with NodeJS.
I need to do a kind of getImage that returns an image corresponding to the name into the url (ex: localhost:8080/getImage/myImg).
I found many tutorials on the internet but the problem is I mustn't use the writeFile and readFile from FS, because my images are stored into an Array with other JSON informations. The images that I'm storing are sent by another API in tomCat and my job is to limitate the calls to the tomCat API by setting up a NodeJS API.
My question is how to send an image stored into a variable/array/json ?
I tried the code below but when I'm going to localhost:8080/getImage/myImg, the code is executed, but nothing is returned. I didn't try to use a kind of tmp folder where I'm sotring images because I'm working on a big product and if I can limitate access to the disk it would be great.
app.get('/getImage/:name', (req, res) => {
imagesArray.forEach( (image) => {
if(req.params.name === image.small_image_name) {
res.writeHead(200, {'content-type': 'image/jpeg'})
res.end(image.small_image, 'binary')
} else if (req.params.name === image.full_image_name) {
res.writeHead(200, {'content-type': 'image/jpeg'})
res.end(image.full_image, 'binary')
}
})
})
The thing I would make is : you do an http request to the url with an image name as parameter, to "download" the image and change the background of an element displayed on my main page.
Thank you for your future answers. have a nice day !