1

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?

1

1 Answer 1

2

Presuming you are using nodejs to render your html file, there are 3 ways:

  1. when rendering the html, render the js code which contains the data as well, eg. JSON
  2. 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
  3. which is similar to option 2, use REST API.
Sign up to request clarification or add additional context in comments.

2 Comments

Note: express is a good option to quickly put together a REST service (which I’d highly recommend in this case)
@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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.