0

I`m new to web development. I need to create an html file in which a paragraph displays the content of a text file.

I created a function in node which reads a text file

function getTextContent(textFilePath) {
    return require("fs").readFileSync(textFilePath, 'utf8');
}

and I plug it into the HTML code


    <body>
    <h1>
    Hello World
    </h1>
    <p> This is some text </p>
    <script type="text/javascript">
    function getTextContent(textFilePath) {
        return require("fs").readFileSync(textFilePath, 'utf8');
    }
    document.write(getTextContent(myFilePath).toString())
    </script>
    </body>

I would expect the text of the file to be displated below "This is some text", however I get nothing.

Can someone help? Thanks

  • You said "with Node" in the question title and then "plug it into the HTML code" — A web browser is not Node.js! – Quentin Sep 02 '19 at 15:06

1 Answers1

0

The JavaScript you put in your page executes client-side, in the browser. The browser doesn't have modules like fs and certainly can't read arbitrary text files from disk.

Sounds like what you need is some server-side code and a template engine.

Brad
  • 146,404
  • 44
  • 300
  • 476