문제

I want to set a javascript variable on my index.html page when the page is returned by express.js. I am trying to use the express-expose middleware but I am not sure how to get variable set in the static html page that is rendered.

On the server I have:

app.get('/', function(req, res) {
    var user = { name: 'tj' };
    res.expose(user, 'user');
    res.render(config.rootPath + '/public/index.html');
});

Index html I have

<script>
  var myUser = user;
</script>

What I need is myUser to contain the json object user.

Examples I have seen use jade which i am not using.

도움이 되었습니까?

해결책

You need to expose them like this

app.expose('var some = "variable";');

https://github.com/visionmedia/express-expose#raw-javascript

The way you're doing it, they're only available to the templating engine (jade). In which case you'll have to do something like this

script var some = #{variable}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top