I'm trying to display all of the images in a directory. The images are uploaded and I want to display them accordingly. However, with my current code mt page only ever generates one image element, and the link to that image is broken. Since I'm new to javascript and nodejs in general, there's probably some extra step that I'm missing. Any help is greatly appreciated.
function show(response, request) {
console.log("Request handler 'show' was called.");
fs.readdir("./tmp", function(error, files) {
response.writeHead(200, {'content-type': 'image/png'});
if (error) {
console.error(error.message);
} else {
files.forEach(function(file) {
response.write("./tmp/" + file, 'binary');
console.log('Image: ./tmp/' + file +' written');
});
response.end();
}
});
console.log('All images written');
}
Currently you are responding with image/png multiple images, your browser doesn't know what to do with all of it, you must have a html markup and display images with tag, so if you have handler for static content place your images in a directory that your webserver can serve for example localhost:3000/images/img1.png (if you are running your server on localhost 3000), change your response content-type to text/html, and for every iteration do response.write(')