Hello I am trying to display images from a folder in my project using the "fs" module with NodeJS,. I need to read all images in a directory and run them in a loop for them to be displayed. I was able to do it but I'm not sure if this is the proper or good way to do it. Thanks in advance for your help.
I inserted my readdir (Reading Files Asynchronously) inside my Homepage route. The images were displayed but I have no one to ask if I did it the proper way. Please help, thank you!
const express = require('express');
const router = express.Router();
const fs = require('fs');
router.get('/', function(){
fs.readdir('./assets/images/', (err, files) => {
if(err) {
throw err;
}
res.render('home', {
files: files
});
});
});
module.exports = router;