gpt4 book ai didi

javascript - Node.js 和 Express : How to create many app. 通过 "for loop"与 Express.js 进行调用?

转载 作者:行者123 更新时间:2023-12-03 01:50:27 24 4
gpt4 key购买 nike

在我的 server.js 中,我尝试循环遍历具有不同 url 的数组,并将这些 url 用于 app.get 请求函数。

这是我的代码:

let articleUrlArray = [ 'https://techcrunch.com/2018/05/19/shared-housing-startups-are-taking-off/',
'https://techcrunch.com/2018/05/19/shared-housing-startups-are-taking-off/',
'https://techcrunch.com/2018/05/19/my-data-request-lists-guides-to-get-data-about-you/',
'https://techcrunch.com/2018/05/19/siempos-new-app-will-break-your-smartphone-addiction/',
'https://techcrunch.com/2018/05/19/la-belle-vie-wants-to-compete-with-amazon-prime-now-in-paris/',
'https://techcrunch.com/2018/05/19/apple-started-paying-15-billion-european-tax-fine/',
'https://techcrunch.com/2018/05/19/original-content-dear-white-people/',
'https://techcrunch.com/2018/05/19/meet-the-judges-for-the-tc-startup-battlefield-europe-at-vivatech/',
'https://techcrunch.com/2018/05/18/nasas-newest-planet-hunting-satellite-takes-a-stellar-first-test-image/',
'https://techcrunch.com/video-article/turning-your-toys-into-robots-with-circuit-cubes/',
'https://techcrunch.com/2018/05/18/does-googles-duplex-violate-two-party-consent-laws/' ];

for(var i = 0; i < articleUrlArray.length-1; i++) {
app.get('/news/news-desc', function(req, res) {

var data = '';

var techCrunchNewsItems = [];

request( articleUrlArray[i], function(err, response, html) {
var $ = cheerio.load(html);

if($('.article-content').children('p').eq(0).text().split(' ').length > 50) {

techCrunchNewsItems.push({
bodyOne: $('.article-content').children('p').eq(0).text()
});
} else {

techCrunchNewsItems.push({
bodyOne: $('.article-content').children('p').eq(0).text(),
bodyTwo: $('.article-content').children('p').eq(1).text()
});
}

data = techCrunchNewsItems;

res.send(JSON.stringify(data));
});
})
}

正如您在我的代码中看到的,我有一个数组调用“articleUrlArray”,并创建了“for 循环”来循环该数组以获取每个“articleUrl”。然后使用该“articleUrl”作为请求函数并获取该 url 的正文内容。

无论发生什么,我总是“只”获取最后一个网址的正文内容。它没有获取“articleUrlArray”中每个网址的正文内容。

我做错了什么?

这是我运行 Hugo Nasciutti 的解决方案后得到的屏幕截图: enter image description here

最佳答案

const articleUrlArray = [
'https://techcrunch.com/2018/05/19/shared-housing-startups-are-taking-off/',
'https://techcrunch.com/2018/05/19/shared-housing-startups-are-taking-off/',
'https://techcrunch.com/2018/05/19/my-data-request-lists-guides-to-get-data-about-you/',
'https://techcrunch.com/2018/05/19/siempos-new-app-will-break-your-smartphone-addiction/',
'https://techcrunch.com/2018/05/19/la-belle-vie-wants-to-compete-with-amazon-prime-now-in-paris/',
'https://techcrunch.com/2018/05/19/apple-started-paying-15-billion-european-tax-fine/',
'https://techcrunch.com/2018/05/19/original-content-dear-white-people/',
'https://techcrunch.com/2018/05/19/meet-the-judges-for-the-tc-startup-battlefield-europe-at-vivatech/',
'https://techcrunch.com/2018/05/18/nasas-newest-planet-hunting-satellite-takes-a-stellar-first-test-image/',
'https://techcrunch.com/video-article/turning-your-toys-into-robots-with-circuit-cubes/',
'https://techcrunch.com/2018/05/18/does-googles-duplex-violate-two-party-consent-laws/'
];

const checkBody = res => (err, response, html) => {
const $ = cheerio.load(html);
const articleContent = $('.article-content').children('p')
const bodyOne = articleContent.eq(0).text()
const bodyTwo = articleContent.eq(1).text()
const isExtensive = bodyOne.split(' ').length > 50
res(isExtensive ? { bodyOne } : { bodyOne, bodyTwo })
}

const getArticle = article => new Promise(res => request(article, checkBody(res)))

app.get('/news/news-desc', (req, res) => {
Promise.all(articleUrlArray.map(getArticle)).then(data => res.send(JSON.stringify(data)))
})

这里真正发生的事情是,我使用一个函数来带来一个 Promise 数组,当所有 Promise 都得到解决时,然后用字符串化的对象数组响应请求。我擅自实现了箭头函数和常量。

关于javascript - Node.js 和 Express : How to create many app. 通过 "for loop"与 Express.js 进行调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50438599/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com