I want to use a variable which is declared in javascript file to a ejs file.
javascript:
var express = require(\'express\');
var app = express();
va
The app.locals.myVar
approach should work, so something must be getting in the way. But you could avoid using app.locals.myVar
altogether and pass variables directly to your views with:
var express = require('express');
var app = express();
app.get('/', function(req, res) {
var myVar = 1;
res.render('testPage', { myVar : myVar });
});
The myVar
variable should now be available to the "testPage" ejs file. Inside of it you could do:
<%= myVar %>
And see it output "1".
Lastly, make sure you have set the view engine to ejs:
app.set('view engine', 'ejs');
Otherwise it won't work.