简体   繁体   English

通过Express从Node.js获取数据

[英]Get data from Node.js via Express

I have a server with Node.js and I use Express to build a web app. 我有一台带有Node.js的服务器,并且使用Express来构建一个Web应用程序。 My server is already able to get an array from a database using a function (rss_interrogDB). 我的服务器已经可以使用函数(rss_interrogDB)从数据库中获取数组。 Now I want to use this array to display a list in the html page. 现在,我想使用此数组在html页面中显示列表。 But I must be missing something... 但是我一定很想念...

On the server-side my code is: 在服务器端,我的代码是:

app.get('/', function(req, res) { 
rss_interrogDB(function() {
// don't konw what to add here
});

On the html page the code is: 在html页面上,代码为:

$.get('/', {}, function(data){
// not sure it is the right code
console.log(data);
// then the code to create a list from the array (no pb with that)
});

Thank you for your help! 谢谢您的帮助!

Assuming your DB gives you an array of data, add something like this to your server code: 假设您的数据库为您提供了一系列数据,请在服务器代码中添加如下内容:

app.get('/', function(req, res) { 

    rss_interrogDB(function(serverData) {
        res.send(serverData);             
    });

});

You could manipulate the serverData object and build one that is more suitable for the client before you send it back, but that depends on what you want to do. 您可以操纵serverData对象并构建一个更适合客户端的对象,然后再将其发送回去,但这取决于您要执行的操作。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM