0

I have the below codes, I am trying to access data sent from my backend server in my frontend js. How can I do that?

Express

...
app.get("/", (req, res) => {
  res.render("home", {data} );
});
...

home.ejs

...
<script src="frontend.js"></script>
...

frontend.js

var data = <%=data%>; // this didnt work...
1

1 Answer 1

0

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

<% if (data) { %>
     <h2>I have data</h2>
     <script>
        var data = <%= data %>            
     </script>
<% } %>

Another option might be to make an AJAX call back to the server once the page has already loaded, return the data

Sign up to request clarification or add additional context in comments.

1 Comment

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.