How to access the data returned by node in the js file of the foreground

node.js: res.render ("index.html", {data:data}) can render the data directly during rendering, but I want to return to the foreground and use the data in the js file, such as js: console.log (data.name), how to access

Mar.12,2021

just do a variable forwarding. The script section sets these variables under window, and the external js directly accesses the variables


in the rendered html text by setting some variables in the script tag, but it will pollute the global variables. You can add a namespace


are you talking about ejs templates?

<% if (gameState) { %>
     <h2>I have a game state!</h2>
     <script>
        var clientGameState = <%= gameState %>            
     </script>
<% } %>

https://stackoverflow.com/que.


-at present, I can access the data from the background directly in the html template
directly in the script tag (without <% > package)
< script >
var articleData= <%-JSON.stringify (articleData)% >;
console.log (articleData.title)
< / script >
my article is a json object
so that it can be accessed, but it is not possible to write it in the imported external js file. It is always possible not to report the wrong use for the time being.


you can do two interfaces on the back end, one using res.render () to send html, to the browser and the other using res.json ({data:data}) to send json data to the browser.


students, template engine to learn about ~

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1bef799-31ca1.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1bef799-31ca1.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?