System File fs Module


#1

if I want to traverse through the file folder using the following fs module and readdirSync method on the server, how can I render this into the html file? I want to access the file system from the user interface form…

 var fs = require('fs');
 var traverseFileSystem = function (currentPath) {
    console.log(currentPath);
    var files = fs.readdirSync(currentPath);
    for (var i in files) {
       var currentFile = currentPath + '/' + files[i];
       var stats = fs.statSync(currentFile);
       if (stats.isFile()) {
       console.log(currentFile);
       }
      else if (stats.isDirectory()) {
             traverseFileSystem(currentFile);
           }
     }
   };
  traverseFileSystem('..');

#3

Generally speaking, this is a little beyond the scope of this front end class / thread.

That said, to serve files to a user, we need first a server to listen for requests, routes to be triggered by different requests and these routes can respond with HTML, CSS or JavaScript files back to the browser. Without a server, this won’t work from a browser, to my knowledge. Something needs to be listening for browser requests. E.g. NGNX, Apache, etc.


#4

So, in this section we can only address the front end HTML/CSS/JavaScript programming (only browser-related) questions?


#5

For the most part yes that would be ideal. For all other subjects, I think it’s best to use the General channel.

If you want to setup a quick and easy server, PHP is very good choice for this. If you want to learn for future dev jobs, Python is likely the most popular language out there and it’s knowledge is highly regarded by companies. That said, JavaScript on the server side may even be more popular at this point, though much more complex than Python to use, due to it’s asynchronous code.

The code you have above is good, in a sense that you can use it to retrieve files from your directory, and then use other code to send the files back to the client as a response.

It’s just that it doesn’t have a web server setup to expect the initial requests from the server, for it to respond to. Hope this helps a little!


#6

Hey @likejean I moved the thread to General, thanks!