In PHP I used to pass objects from the backend to the front end in JSON form, using script tags.
<script>
var serversideStuff = '<?php echo json_encode($serversideArray); ?>';
</script>
How can I pass JS objects from the server-side to the clientside using nodeJS, express and Jade. There is probably a really good way that I'm just not aware of.
Hope you can help me out.
In PHP I used to pass objects from the backend to the front end in JSON form, using script tags.
<script>
var serversideStuff = '<?php echo json_encode($serversideArray); ?>';
</script>
How can I pass JS objects from the server-side to the clientside using nodeJS, express and Jade. There is probably a really good way that I'm just not aware of.
Hope you can help me out.
Share Improve this question asked Oct 6, 2011 at 17:04 wilsonpagewilsonpage 17.6k23 gold badges105 silver badges150 bronze badges 02 Answers
Reset to default 5In PHP you use bad practices (dynamically generating javascript as part of an application).
With node you do it right. This means you
- Either write the data to the HTML (How would your website work without javascript, you are using progressive enhancement, right?)
- Expose that data as a web service you talk to over Ajax or WebSockets
<script type="text/javascript">
var clientVar = {{JSON.stringify(serverVar)}}
</script>
change {{}} to whatever your template engine uses to interpret code