Obama nation
Obama nation

Reputation: 411

How to send data from a nodejs file to an html page

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?

Upvotes: 1

Views: 333

Answers (1)

James Lin
James Lin

Reputation: 26538

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.

Upvotes: 2

Related Questions