I am accessing files from nodejs dynamically, but I am facing a problem when user creates a directory with different case like (/hello) and try to access it with different case like (/Hello). Is there a way that I can access directories on linux through nodejs with case insensitivity?
Advertisement
Answer
On a case-insensitive filesystem your best bet is probably going to be just reading the list of files in the requested file’s directory and do a case-insensitive comparison. For example:
var requestedPath = '...'; fs.readdir(path.dirname(requestedPath), (err, names) => { if (err) throw err; var requestedFile = path.basename(requestedPath).toLowerCase(); for (var i = 0; i < names.length; ++i) { if (names[i].toLowerCase() === requestedFilename) // do something } });