Accessing EJS variable in Javascript logic

后端 未结 6 482
不知归路
不知归路 2020-12-02 20:21

I\'m working on a Node.js app (it\'s a game). In this case, I have some code set up such that when a person visits the index and chooses a room, he gets redirected to the pr

6条回答
  •  情歌与酒
    2020-12-02 21:03

    You could directly inject the gameState variable into javascript on the page.

    <% if (gameState) { %>
         

    I have a game state!

    <% } %>

    Another option might be to make an AJAX call back to the server once the page has already loaded, return the gameState JSON, and set clientGameState to the JSON response.

    You may also be interested in this: How can I share code between Node.js and the browser?

提交回复
热议问题