Organizational Research By

Surprising Reserch Topic

pass variables to javascript in expressjs


pass variables to javascript in expressjs  using -'javascript,node.js,express,ejs'

I am completely lost on this; I am using NodeJS to fetch a JSON and I need to pass the variable to my page and have JavaScript use the data.

app.get('/test', function(req, res) {
    res.render('testPage', {
        myVar: 'My Data'
    });


That is my Express code (very simple for testing purposes); now using EJS I want to gather this data which I know to render on the page is simply

<%= myVar %>


But I need to be able to gather this data in JavaScript (if possible within a .js file) but for now just to display the variable in an Alert box I have tried

In Jade it is like alert('!{myVar}') or !{JSON.stringify(myVar)}. Can I do something similar in EJS. I don't need any field like <input type=hidden> and taking the value of the field in javascript. If anyone can help be much appreciated
    

asked Sep 29, 2015 by kotmus2002
0 votes
3 views



Related Hot Questions

3 Answers

0 votes
NULL
answered Sep 29, 2015 by tushar2k6
0 votes
NULL
answered Sep 29, 2015 by 20shahi
0 votes
NULL
answered Sep 29, 2015 by tseetha

...