I have a few plain text files from which I would like to take some strings and render them into an HTML file. The problem is that since nodeJs, which is what I am using to parse file data, is a server-side technology, I can't just link the nodeJs file to the html with something like <script type="text/javascript" src="./homepage.js"></script> and call something like document.querySelectorAll('area'); in the javascript file to get an element whose content I can customize. I have a nodeJs file that retrieves and manipulates data taken from the files. I want to manipulate this data and render it into the html file as I would usually do for a regular javascript file (using query selectors and the such). So how might I be able to take some data from my file system and render it into a certain html file?
-
Does this answer your question? stackoverflow.com/questions/6084360/…Emre Koc– Emre Koc2020-07-30 03:26:09 +00:00Commented Jul 30, 2020 at 3:26
Add a comment
|
1 Answer
Presuming you are using nodejs to render your html file, there are 3 ways:
- when rendering the html, render the js code which contains the data as well, eg. JSON
- your html can include
<script type="text/javascript" src="http://your_nodejs_server/data.js"></script>which will fires a request to your nodejs server which will render the js file containing your data dynamically - which is similar to option 2, use REST API.
2 Comments
Obama nation
@p32094 When I do that second option, I get an error for the nodeJs javascript file of something like "error on line 1, 'require' is not defined'. The way I was doing this was when I put my HTML, css, and nodeJs file in a static folder while using the express.static() function. To clarify, the HTML and nodeJs javascript file are in the same static folder