To put it simply, I’m trying to create a series of buttons from the names of files in a folder located on the server (here it is called “CAD”). I know that a JavaScript running client-side cannot access server folders on the server file system. What is the best way to make this happen? I am a beginner to this so any help would be very appreciated.
I have tried using PHP, but that only allows me to access local folders on my laptop. When I host my web-app, I want to be able to access the files in folders in the server. I have also tried used a Node.JS environment, but this also does not work very well.
const fs = require("fs").promises;
function main() {
findFileTypes("CAD", (fileNames, fileTypes) => {
console.log(fileNames);
console.log(fileTypes);
});
}
main();
function findFileTypes(directory, callback) {
fs.readdir(directory)
.then(fileNames => {
const fileTypes = fileNames.map(fileName => {
const dotIndex = fileName.lastIndexOf(".");
if (dotIndex > 0) {
return fileName.substring(dotIndex);
} else {
return "";
}
});
callback(fileNames, fileTypes);
})
.catch(error => {
console.error("Error reading directory:", error);
});
}