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?
Asked
Active
Viewed 286 times
1

Obama nation
- 191
- 1
- 1
- 6
-
Does this answer your question? https://stackoverflow.com/questions/6084360/using-node-js-as-a-simple-web-server ā Emre Koc Jul 30 '20 at 03:26
1 Answers
2
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.

James Lin
- 25,028
- 36
- 133
- 233
-
Note: express is a good option to quickly put together a REST service (which Iād highly recommend in this case) ā Jul 30 '20 at 05:24
-
@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 ā Obama nation Jul 30 '20 at 15:14