My website is serving a lot of pictures from /assets/photos/
folder. How can I get a list of the files in that folder with Javascript?
To get a list of the names of all files present in a directory in Node. js, we can call the readdir method. const testFolder = './folder/path'; const fs = require('fs'); fs. readdir(testFolder, (err, files) => { files.
The current code will give a list of all files in a folder, assuming it's on the server side you want to list all files:
var fs = require('fs'); var files = fs.readdirSync('/assets/photos/');
No, Javascript doesn't have access to the filesystem. Server side Javascript is a whole different story but I guess you don't mean that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With