简体   繁体   English

如何将JS对象从服务器端传递到NodeJS中的客户端

[英]How can I pass JS objects from the server-side to the clientside in NodeJS

In PHP I used to pass objects from the backend to the front end in JSON form, using script tags. 在PHP中,我曾经使用脚本标记以JSON形式将对象从后端传递到前端。

<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. 如何使用nodeJS,express和Jade将JS对象从服务器端传递到客户端。 There is probably a really good way that I'm just not aware of. 可能有一种我不知道的好方法。

Hope you can help me out. 希望你能帮助我。

In PHP you use bad practices (dynamically generating javascript as part of an application). 在PHP中,您使用不良做法(动态生成javascript作为应用程序的一部分)。

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?) 将数据写入HTML(如果没有javascript,您的网站如何工作,您使用渐进式增强,对吧?)
  • Expose that data as a web service you talk to over Ajax or WebSockets 将该数据公开为您通过Ajax或WebSockets与之交谈的Web服务
<script type="text/javascript">
  var clientVar = {{JSON.stringify(serverVar)}}
</script>

change {{}} to whatever your template engine uses to interpret code 将{{}}更改为模板引擎用于解释代码的任何内容

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在服务器端执行JS? - How can I execute JS on server-side? 如何将js中的变量从服务器端传递到nodejs中的客户端 - How to pass variable in js from serverside to clientside in nodejs 如何将数据从客户端表单输入传输到服务器端Nodejs脚本? - How do I transfer data from a client-side form input to a server-side Nodejs script? 如何从nodejs服务器端的表单标签记录数据? - how to log datas from form tags on nodejs server-side? 我应该如何以可靠地用于转换为其他时区的方式在服务器端存储来自JS的时间戳? - How should I store a timestamp coming from JS on the server-side in a way that I can reliably use to convert to other timezones? 如何使用 ajax 调用将 jQuery 中网格中已删除的行名传递给控制器(服务器端) - How do I pass the deleted row name from a grid in jQuery to controller(server-side) with ajax call 如何将反应前端的objectId传给服务端graphql? - How do I pass in objectId from the react front end to server-side graphql? 如何从服务器端向客户端发送变量? - How can I send a variable from the Server-side to the Client-side? 如何使用meteor 在服务器端main.js 上将数据插入到我的集合中? - How can I insert data to my collection on my server-side main.js with meteor? 如何使用 express.js 和 firebase 创建服务器端身份验证系统? - How can i create a server-side authentication system with express.js and firebase?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM