Unable to use variables in fs functions when using brfs












0















I use browserify in order to be able to use require. To use fs functions with browserify i need to transform it with brfs but as far as I understood this results in only being able to input static strings as parameters inside my fs function. I want to be able to use variables for this.



I want to search for xml files in a specific directory and read them. Either by searching via text field or showing all of their data at once. In order to do this I need fs and browserify in order to require it.



const FS = require('fs')
function lookForRoom() {
let files = getFileNames()
findSearchedRoom(files)
}
function getFileNames() {
return FS.readdirSync('../data/')

}
function findSearchedRoom(files) {
const SEARCH_FIELD_ID = 'room'
let searchText = document.getElementById(SEARCH_FIELD_ID).value
files.forEach((file) => {
const SEARCHTEXT_FOUND = file.includes(searchText.toLowerCase())
if (SEARCHTEXT_FOUND) loadXML(file)
})
}
function loadXML(file) {
const XML2JS = require('xml2js')
let parser = new XML2JS.Parser()
let data = FS.readFile('../data/' + file)
console.dir(data);
}
module.exports = { lookForRoom: lookForRoom }


I want to be able to read contents out of a directory containing xml files.
Current status is that I can only do so when I provide a constant string to the fs function










share|improve this question



























    0















    I use browserify in order to be able to use require. To use fs functions with browserify i need to transform it with brfs but as far as I understood this results in only being able to input static strings as parameters inside my fs function. I want to be able to use variables for this.



    I want to search for xml files in a specific directory and read them. Either by searching via text field or showing all of their data at once. In order to do this I need fs and browserify in order to require it.



    const FS = require('fs')
    function lookForRoom() {
    let files = getFileNames()
    findSearchedRoom(files)
    }
    function getFileNames() {
    return FS.readdirSync('../data/')

    }
    function findSearchedRoom(files) {
    const SEARCH_FIELD_ID = 'room'
    let searchText = document.getElementById(SEARCH_FIELD_ID).value
    files.forEach((file) => {
    const SEARCHTEXT_FOUND = file.includes(searchText.toLowerCase())
    if (SEARCHTEXT_FOUND) loadXML(file)
    })
    }
    function loadXML(file) {
    const XML2JS = require('xml2js')
    let parser = new XML2JS.Parser()
    let data = FS.readFile('../data/' + file)
    console.dir(data);
    }
    module.exports = { lookForRoom: lookForRoom }


    I want to be able to read contents out of a directory containing xml files.
    Current status is that I can only do so when I provide a constant string to the fs function










    share|improve this question

























      0












      0








      0








      I use browserify in order to be able to use require. To use fs functions with browserify i need to transform it with brfs but as far as I understood this results in only being able to input static strings as parameters inside my fs function. I want to be able to use variables for this.



      I want to search for xml files in a specific directory and read them. Either by searching via text field or showing all of their data at once. In order to do this I need fs and browserify in order to require it.



      const FS = require('fs')
      function lookForRoom() {
      let files = getFileNames()
      findSearchedRoom(files)
      }
      function getFileNames() {
      return FS.readdirSync('../data/')

      }
      function findSearchedRoom(files) {
      const SEARCH_FIELD_ID = 'room'
      let searchText = document.getElementById(SEARCH_FIELD_ID).value
      files.forEach((file) => {
      const SEARCHTEXT_FOUND = file.includes(searchText.toLowerCase())
      if (SEARCHTEXT_FOUND) loadXML(file)
      })
      }
      function loadXML(file) {
      const XML2JS = require('xml2js')
      let parser = new XML2JS.Parser()
      let data = FS.readFile('../data/' + file)
      console.dir(data);
      }
      module.exports = { lookForRoom: lookForRoom }


      I want to be able to read contents out of a directory containing xml files.
      Current status is that I can only do so when I provide a constant string to the fs function










      share|improve this question














      I use browserify in order to be able to use require. To use fs functions with browserify i need to transform it with brfs but as far as I understood this results in only being able to input static strings as parameters inside my fs function. I want to be able to use variables for this.



      I want to search for xml files in a specific directory and read them. Either by searching via text field or showing all of their data at once. In order to do this I need fs and browserify in order to require it.



      const FS = require('fs')
      function lookForRoom() {
      let files = getFileNames()
      findSearchedRoom(files)
      }
      function getFileNames() {
      return FS.readdirSync('../data/')

      }
      function findSearchedRoom(files) {
      const SEARCH_FIELD_ID = 'room'
      let searchText = document.getElementById(SEARCH_FIELD_ID).value
      files.forEach((file) => {
      const SEARCHTEXT_FOUND = file.includes(searchText.toLowerCase())
      if (SEARCHTEXT_FOUND) loadXML(file)
      })
      }
      function loadXML(file) {
      const XML2JS = require('xml2js')
      let parser = new XML2JS.Parser()
      let data = FS.readFile('../data/' + file)
      console.dir(data);
      }
      module.exports = { lookForRoom: lookForRoom }


      I want to be able to read contents out of a directory containing xml files.
      Current status is that I can only do so when I provide a constant string to the fs function







      node.js requirejs browserify brfs






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 1 at 21:44









      DaksonDakson

      2516




      2516
























          1 Answer
          1






          active

          oldest

          votes


















          2














          The brfs README contains this gotcha:




          Since brfs evaluates your source code statically, you can't use dynamic expressions that need to be evaluated at run time.




          So, basically, you can't use brfs in the way you were hoping.




          I want to be able to read contents out of a directory containing xml files




          If by "a directory" you mean "any random directory, the name of which is determined by some form input", then that's not going to work. Browsers don't have direct access to directory contents, either locally or on a server.



          You're not saying where that directory exists. If it's local (on the machine the browser is running on): I don't think there are standardized API's to do that, at all.



          If it's on the server, then you need to implement an HTTP server that will accept a directory-/filename from some clientside code, and retrieve the file contents that way.






          share|improve this answer
























          • So the only viable way is to do it with some request that calls a lets say php script that provides the contents of the xml?

            – Dakson
            Jan 1 at 22:58













          • @Dakson yes, something running on the server at least.

            – robertklep
            Jan 2 at 6:40











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999183%2funable-to-use-variables-in-fs-functions-when-using-brfs%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          The brfs README contains this gotcha:




          Since brfs evaluates your source code statically, you can't use dynamic expressions that need to be evaluated at run time.




          So, basically, you can't use brfs in the way you were hoping.




          I want to be able to read contents out of a directory containing xml files




          If by "a directory" you mean "any random directory, the name of which is determined by some form input", then that's not going to work. Browsers don't have direct access to directory contents, either locally or on a server.



          You're not saying where that directory exists. If it's local (on the machine the browser is running on): I don't think there are standardized API's to do that, at all.



          If it's on the server, then you need to implement an HTTP server that will accept a directory-/filename from some clientside code, and retrieve the file contents that way.






          share|improve this answer
























          • So the only viable way is to do it with some request that calls a lets say php script that provides the contents of the xml?

            – Dakson
            Jan 1 at 22:58













          • @Dakson yes, something running on the server at least.

            – robertklep
            Jan 2 at 6:40
















          2














          The brfs README contains this gotcha:




          Since brfs evaluates your source code statically, you can't use dynamic expressions that need to be evaluated at run time.




          So, basically, you can't use brfs in the way you were hoping.




          I want to be able to read contents out of a directory containing xml files




          If by "a directory" you mean "any random directory, the name of which is determined by some form input", then that's not going to work. Browsers don't have direct access to directory contents, either locally or on a server.



          You're not saying where that directory exists. If it's local (on the machine the browser is running on): I don't think there are standardized API's to do that, at all.



          If it's on the server, then you need to implement an HTTP server that will accept a directory-/filename from some clientside code, and retrieve the file contents that way.






          share|improve this answer
























          • So the only viable way is to do it with some request that calls a lets say php script that provides the contents of the xml?

            – Dakson
            Jan 1 at 22:58













          • @Dakson yes, something running on the server at least.

            – robertklep
            Jan 2 at 6:40














          2












          2








          2







          The brfs README contains this gotcha:




          Since brfs evaluates your source code statically, you can't use dynamic expressions that need to be evaluated at run time.




          So, basically, you can't use brfs in the way you were hoping.




          I want to be able to read contents out of a directory containing xml files




          If by "a directory" you mean "any random directory, the name of which is determined by some form input", then that's not going to work. Browsers don't have direct access to directory contents, either locally or on a server.



          You're not saying where that directory exists. If it's local (on the machine the browser is running on): I don't think there are standardized API's to do that, at all.



          If it's on the server, then you need to implement an HTTP server that will accept a directory-/filename from some clientside code, and retrieve the file contents that way.






          share|improve this answer













          The brfs README contains this gotcha:




          Since brfs evaluates your source code statically, you can't use dynamic expressions that need to be evaluated at run time.




          So, basically, you can't use brfs in the way you were hoping.




          I want to be able to read contents out of a directory containing xml files




          If by "a directory" you mean "any random directory, the name of which is determined by some form input", then that's not going to work. Browsers don't have direct access to directory contents, either locally or on a server.



          You're not saying where that directory exists. If it's local (on the machine the browser is running on): I don't think there are standardized API's to do that, at all.



          If it's on the server, then you need to implement an HTTP server that will accept a directory-/filename from some clientside code, and retrieve the file contents that way.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 1 at 22:02









          robertkleprobertklep

          138k18235245




          138k18235245













          • So the only viable way is to do it with some request that calls a lets say php script that provides the contents of the xml?

            – Dakson
            Jan 1 at 22:58













          • @Dakson yes, something running on the server at least.

            – robertklep
            Jan 2 at 6:40



















          • So the only viable way is to do it with some request that calls a lets say php script that provides the contents of the xml?

            – Dakson
            Jan 1 at 22:58













          • @Dakson yes, something running on the server at least.

            – robertklep
            Jan 2 at 6:40

















          So the only viable way is to do it with some request that calls a lets say php script that provides the contents of the xml?

          – Dakson
          Jan 1 at 22:58







          So the only viable way is to do it with some request that calls a lets say php script that provides the contents of the xml?

          – Dakson
          Jan 1 at 22:58















          @Dakson yes, something running on the server at least.

          – robertklep
          Jan 2 at 6:40





          @Dakson yes, something running on the server at least.

          – robertklep
          Jan 2 at 6:40




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999183%2funable-to-use-variables-in-fs-functions-when-using-brfs%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas

          Can't read property showImagePicker of undefined in react native iOS