0

Is it possible to return the contents of a static path to a directory instead of using an .

I want to write a script that reads the contents of a directory on the file system to a given time daily. This is integrated in a webapp I can't edit.

  • The post is not really understandable in its current form. "Return contents of a static path to a directory" does not make any sense. Writing a script that reads the contents of a directory is doable of course. But should it be a a part of a a webapp you can't edit? If it should be there and you cannot edit it, then well... Also there is no question asked. – simon Sep 25 '20 at 08:14
  • know what a plugin is? – CrazyCurryWurst Sep 25 '20 at 08:23
  • To get good answers you should write all relevant information in the question, e.g. if you are writing a plugin. Before posting try to read your question from the viewpoint of someone how is not familiar with your project and make edits accordingly. – simon Sep 25 '20 at 08:45

1 Answers1

0

Short answer: you can't. You need to do this server-side. Here is an answer from a similar question, using node.js.


You can use the fs.readdir or fs.readdirSync methods.

fs.readdir

const testFolder = './tests/';
const fs = require('fs');

fs.readdir(testFolder, (err, files) => {
  files.forEach(file => {
    console.log(file);
  });
});

fs.readdirSync

const testFolder = './tests/';
const fs = require('fs');

fs.readdirSync(testFolder).forEach(file => {
  console.log(file);
});

The difference between the two methods, is that the first one is asynchronous, so you have to provide a callback function that will be executed when the read process ends.

The second is synchronous, it will return the file name array, but it will stop any further execution of your code until the read process ends.

PatricNox
  • 3,306
  • 1
  • 17
  • 25
  • Answer from: https://stackoverflow.com/questions/2727167/how-do-you-get-a-list-of-the-names-of-all-files-present-in-a-directory-in-node-j – PatricNox Sep 25 '20 at 08:11
  • Hi PatricNox this only works for node I guess? I can use Node but I am totally new to this. It is not possible to do the same with plain JavaScript? If I use Node how can I pass the data from this function to the web client? – CrazyCurryWurst Sep 25 '20 at 08:42
  • Yes, sorry forgot to mention that. Pure JavaScript can't access your files, you'll need some server rendering for it, like node in this example. – PatricNox Sep 25 '20 at 08:48
  • And is it possible to send this data to a already existing page thats generated by the webapp. For example rendering it in an existing
    container?
    – CrazyCurryWurst Sep 25 '20 at 09:22
  • If it's necessary to call client side function from server then you will need some kind of open connection like Websockets, and on client side listen for a new message from server and react accordingly. – PatricNox Sep 25 '20 at 09:24
  • Okay I will try my best, thank you very much directing me on the right path. I highly appreciate it! :) – CrazyCurryWurst Sep 25 '20 at 09:42
  • No worries @CrazyCurryWurst! – PatricNox Sep 25 '20 at 10:07