In my back end(build by express) I was storing user image in MongoDB using gridfs. Now for testing purpose I want to check if all the images is loaded or not in the client . By this code below, I was able to fetch one single image:
// http://localhost:3000/avater/avatar1 (api call)
app.get('/avater/:filename', (req, res) => {
if (req.params) {
gfs.files.findOne({ filename: req.params.filename }, (err, files) => {
// Error checking
if (!files || files.length === 0) {
return res.status(404).json({
responseCode: 1,
responseMessage: 'error'
});
}
var readstream = gfs.createReadStream({
filename: files.filename
});
// set the proper content type
res.set('Content-Type', files.contentType);
// Return response
return readstream.pipe(res);
});
} else {
return res.status(404).json({ error: 'username is not corrent' });
}
});
Now i just wanted to display all the user avatar on the browser. I dont know how to send multiple images from gridfs to the cliend. That is how i wrote the code bellow. But having errors as:
(node:16704) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 close listeners added. Use emitter.setMaxListeners() to increase limit
// http://localhost:3000/allavater (api url)
app.get('/allavater', (req, res) => {
let count = 0;
let filesData = [];
gfs.files.find({ }).toArray((err, files) => {
// Error checking
if (!files || files.length === 0) {
return res.status(404).json({
responseCode: 1,
responseMessage: 'error'
});
}
// Loop through all the files and fetch the necessary information
files.forEach(file => {
filesData[count++] = {
filename: file.filename,
contentType: file.contentType
};
});
return filesData.map(file => {
var readstream = gfs.createReadStream({
filename: file.filename
});
// set the proper content type
res.set('Content-Type', file.contentType);
// Return response
return readstream.pipe(res);
});
});
});
How can i send all the images to the cliend where browser will display all the iamges stored in the mongo? Besides is there anyway to create a link for each image so that i can display them later on in my Angular project?