I've been struggling with implementing this code on my project, despite there being similar posts on the topic.
Here is the basic rendering function I am using:
router.get("/", function(req, res) {
res.render("index", {
route: "one"
});
});
However, I have been unable to access the route
variable in my client-side JavaScript file. I've tried a few methods, but none seem to be working:
// Unexpected token: <
var route = <%- JSON.stringify(route) %>;
// variable 'route' is undefined
var route = !{route};
// Unexpected token .
var route = !{JSON.stringify(route)};
// console log out "<%- JSON.stringify(route) %>;
var route = "<$- JSON.stringify(route) %>";
It's worth mentioning that my view engine is ejs
.