Skip to content
Advertisement

Passing variable from app.js to ejs file into a script tag

I’m trying to pass variable which is defined in app.js into a script tag inside and ejs file. So the app.js in short looks like this:

JavaScript

The variable I want to pass is the app.locals.myVar, acutally this method doesnt work and I still get that myVar is not defined in ejs file

The ejs file is here:

JavaScript

As you can see im trying to use myVar- the variable that is obtained on the server side js and then using it in the client side js. But it appears to not be recognized in the ejs file.

Advertisement

Answer

res.render() is parameterized. So, try:

JavaScript

See Express docs for: app.render() and res.render().

Your HTML should look something like this with the <%= myVar %> included.

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement