根据获取响应表达来填充数组 [英] Fill an array based on a get response express
问题描述
我有以下server.js获取路由
I have the following server.js get route
app.get('/', function(req, res) {
var url;
var final_res = [];
endpoints.forEach(function(url){
request(url, function(error,response,body){
if(!error && response.statusCode == 200){
final_res.push(url.url);
console.log(url.url);
}else{
res.send(err);
console.log(err);
}
});
});
});
这是我的客户端js,我在其中使用jQuery获取完全相同的内容
And this is my client js where I fetch this exact same get with jQuery
$(document).ready(function() {
$.get('http://localhost:3000/', function(data) {
console.log(data);
$("#body").text(data);
});
});
当我打开index.html时,它会正确显示用户界面,而在执行我的server.js的终端中,它会正确显示URL.我无法完成的工作是如何使用jQuery接收到的数据来填充html中的表.我的表中将填充从端点提取的网址.
When I open my index.html it displays the user interface correctly and inside my terminal where I have executing my server.js it correctly displays the url. What I can't accomplish is how to use my data that my jQuery receives in order to populate a table inside my html. My table will be populated with urls that are fetch from my endpoints.
我在nodejs中有一定的背景,但是我不能总结一下.
I have some background in nodejs but I cant wrap this up.
推荐答案
由于您需要知道何时完成多个请求,因此建议您改用request-promise库,以便可以使用promise来跟踪所有请求请求已完成.该库还会自动为您检查statusCode.因此,您可以执行以下操作:
Since you need to know when multiple requests are done, I'd suggest you switch to using the request-promise library so you can use promises to track when all the requests are done. That library also checks the statusCode for you automatically. So, you can do this:
const rp = require('request-promise');
app.get('/', function(req, res) {
Promise.all(endpoints.map(url => {
return rp(url).then(r => {
return url.url;
}).catch(err => {
// rather than error, just return null result
return null;
})
})).then(results => {
// filter out null values, then send array as the response
res.json(results.filter(item => item !== null));
}).catch(err => {
console.log(err);
res.sendStatus(500);
});
});
这将并行运行所有请求,但是将按顺序收集结果,这将导致最快的总体运行时间.
This will run all the requests in parallel, but collect the results in order which should result in the fastest overall run time.
如果您希望一次运行它们,则可以使用async/await,如下所示:
If you wanted to run them one a time, you could use async/await like this:
const rp = require('request-promise');
app.get('/', async function(req, res) {
let results = [];
for (let url of endpoints) {
try {
let r = await rp(url);
if (r) {
results.push(url.url);
}
} catch(e) {
// ignore error
}
}
res.json(results);
});
编辑2020年1月-维护模式下的request()模块
FYI,request
模块及其派生类,例如 request-promise
现在处于维护模式,不会积极开发以添加新功能.您可以在此处了解更多信息.在此表中有一些替代方案,并进行了一些讨论每个.我一直在自己使用 got()
,它是从一开始就使用诺言而构建的,很简单使用.
FYI, the request
module and its derivatives like request-promise
are now in maintenance mode and will not be actively developed to add new features. You can read more about the reasoning here. There is a list of alternatives in this table with some discussion of each one. I have been using got()
myself and it's built from the beginning to use promises and is simple to use.
这篇关于根据获取响应表达来填充数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!