如何将数据从节点js传递给html。? [英] How to pass data from node js to html.?

查看:82
本文介绍了如何将数据从节点js传递给html。?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何简单地将节点js中的变量传递给html并将其显示在页面中?
什么是简单的机制。
我试图用节点js来开发一个简单的待办事项列表用于查看从节点(服务器端)到html的数据:

1-您可以在节点中创建一个文件,以json的形式返回数据,然后从JQuery中执行ajax调用这个页面并用它替换部分HTML。
节点js中的示例代码:

  var usersFilePath = path.join(__ dirname,'users.min.json' ); 
apiRouter.get('/ users',function(req,res){
var readable = fs.createReadStream(usersFilePath);
readable.pipe(res);
} );

Ajax调用:

  $。get(/ users,function(data){
$(.result).html(data);
alert(Load was performed。) ;
});

2-您可以使用Express与Jade(我推荐
http://expressjs.com/


这是我的博客,开始使用node.js 点击此处
我为nodejs创建了一个入门工具包如果您有兴趣点击这里


How can I simply pass a variable from node js to html and display it in the page? What is the simple mechanism. I am trying to develop a simple to-do list using node js

解决方案

There are two approaches here you can use to view data from node( server side ) to html:

1- You could create a file in node which return data in json, then from JQuery you could do an ajax call this page and replace parts of the HTML with it. Sample code in node js:

var usersFilePath = path.join(__dirname, 'users.min.json');
apiRouter.get('/users', function(req, res){
    var readable = fs.createReadStream(usersFilePath);
    readable.pipe(res);
});

Ajax call:

$.get( "/users", function( data ) {
  $( ".result" ).html( data );
  alert( "Load was performed." );
});

2- You could use express with Jade ( I recommend http://expressjs.com/ )

Here is my blog on how to get started with node.js Click Here I created a starter kit for nodejs If you are interested Click Here

这篇关于如何将数据从节点js传递给html。?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆