How to pass JSON data from Node js route to script inside an html file?

user2994932

I am trying to populate a dropdown in my index html with data from a mongodb collection. I have populating code that looks like this:

    <script>
        document.addEventListener("DOMContentLoaded", function() {
          for (element in mongoData){
            var option = document.createElement('option');
            option.innerHTML = element;
            option.value = element;
            document.getElementById('Active_Sessions').append(option);
          }
        });
      </script>

and in app.js, my route is as follows:

    app.use("/",(req,res) => {
        res.sendFile(__dirname + "/index.html")
     })

I need to pass the JSON object 'mongoData' to this html, and am not sure how to do this. The 'mongoData' will be a JSON of some data collected from a given collection.

One solution I thought of but haven't implemented yet is to write out this mongodata to a text file or a csv file or something like that from the app.js, and then read in the file from my populating script. I feel like this would be bad form though and not very secure.

Another option I thought of is to try and connect to the mongoose server within the populating script, but when I tried this, it didn't work so I've kinda assumed that this is not possible.

Something I've looked into before posting this is using res.render() to send the JSON, but I don't really understand what templating is and would rather not have to learn about all of that if I can avoid that.

Is there an elegant solution to this?

11AND2

You would change your data in your server eg through a template engine.

Or you can build something similar to do the same. Dummy logic:

  1. Readfile with fs module.
  2. Use regex to replace/insert your content (eg from your object).
  3. Send result to client.

UPDATE: A simple example for a custom template engine.

First a simple html file - index.html (please notice $replace_me):

<html>
  <body>
    <div class="circle">
      $replace_me

      <div class="square">
        something else
      </div>
    </div>
  <body>
</html>

In our node server we could do something like this:

var fs = require('fs');

//used sync for clean demo, use async in production
var _html_cont = fs.readFileSync('index.html', {encoding: 'utf-8'});

//dynamic content to replace
var _repl = "<div>I am new!</div>";

//actually replace it
var _im_regex = new RegExp("\\$replace_me", "gm");
_html_cont = _html_cont.replace(_im_regex, _repl);

 //send result to client
 res.writeHead(200, { 'content-type': 'text/html' });
 res.write(_html_cont);
 res.end();

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Pass data from python script to HTML

分類Dev

How to pass json data from C# controller to angular js?

分類Dev

In Chrome Extension development, how do I pass a variable from background.js to a content script that embedded as a script node?

分類Dev

Discord API, can not get data from json array inside array from user input arg[1] in Node JS

分類Dev

Trying to pass data from a .php file to a .js file

分類Dev

How to create json file in node js

分類Dev

How to create json file in node js

分類Dev

How do I display chart on my HTML page based on JSON data sent by Node JS

分類Dev

Pass .json file from html-webpack-plugin to handlebars template

分類Dev

Pass .json file from html-webpack-plugin to handlebars template

分類Dev

How to pass a variable from an html form to a php script?

分類Dev

How to pass variable from Batch File to a Powershell Script

分類Dev

JS : How to pass value from module file to main file

分類Dev

How should I return this remote JSON data from my Node.js module?

分類Dev

How to pass data to a JQuery script

分類Dev

Updating HTML data from JS file (HTML/JQuery)

分類Dev

How to pass a function as a prop into a route React js?

分類Dev

How to get data from JSON file?

分類Dev

How to pass parameters from jQuery to laravel route

分類Dev

Access elements inside JSON (Node js)

分類Dev

How to remove all script tags from html file

分類Dev

Filter json data in node js

分類Dev

Pass parameter from Batch file to MYSQL script

分類Dev

how to pass the json data inside javascript regex? and also validate password in sequence

分類Dev

How to create array from json data in shell script

分類Dev

How to send checkboxes values from HTML to Node JS?

分類Dev

How to get result from function in other file in node.js

分類Dev

How do I read JS as data from a script tag?

分類Dev

How to return JSON from MongoDB in Node.js?

Related 関連記事

  1. 1

    Pass data from python script to HTML

  2. 2

    How to pass json data from C# controller to angular js?

  3. 3

    In Chrome Extension development, how do I pass a variable from background.js to a content script that embedded as a script node?

  4. 4

    Discord API, can not get data from json array inside array from user input arg[1] in Node JS

  5. 5

    Trying to pass data from a .php file to a .js file

  6. 6

    How to create json file in node js

  7. 7

    How to create json file in node js

  8. 8

    How do I display chart on my HTML page based on JSON data sent by Node JS

  9. 9

    Pass .json file from html-webpack-plugin to handlebars template

  10. 10

    Pass .json file from html-webpack-plugin to handlebars template

  11. 11

    How to pass a variable from an html form to a php script?

  12. 12

    How to pass variable from Batch File to a Powershell Script

  13. 13

    JS : How to pass value from module file to main file

  14. 14

    How should I return this remote JSON data from my Node.js module?

  15. 15

    How to pass data to a JQuery script

  16. 16

    Updating HTML data from JS file (HTML/JQuery)

  17. 17

    How to pass a function as a prop into a route React js?

  18. 18

    How to get data from JSON file?

  19. 19

    How to pass parameters from jQuery to laravel route

  20. 20

    Access elements inside JSON (Node js)

  21. 21

    How to remove all script tags from html file

  22. 22

    Filter json data in node js

  23. 23

    Pass parameter from Batch file to MYSQL script

  24. 24

    how to pass the json data inside javascript regex? and also validate password in sequence

  25. 25

    How to create array from json data in shell script

  26. 26

    How to send checkboxes values from HTML to Node JS?

  27. 27

    How to get result from function in other file in node.js

  28. 28

    How do I read JS as data from a script tag?

  29. 29

    How to return JSON from MongoDB in Node.js?

ホットタグ

アーカイブ