Javascript Node.js和Express:如何通过“for loop”创建许多app.get调用?
在我的server.js中,我试图循环遍历具有不同URL的数组,并将这些URL用于app.get请求函数 这是我的密码: 让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/' ]; forvar i=0;iJavascript Node.js和Express:如何通过“for loop”创建许多app.get调用?,javascript,node.js,reactjs,express,Javascript,Node.js,Reactjs,Express,在我的server.js中,我试图循环遍历具有不同URL的数组,并将这些URL用于app.get请求函数 这是我的密码: 让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://techcru
这里真正发生的事情是,我使用一个函数带来一系列承诺,当所有承诺都得到解决时,然后用字符串化的对象数组响应请求。我自由地实现了箭头函数和常量。您应该在app.get'/news/news desc',functionreq,res{…}中执行循环,因为请求需要选择一种方法在循环中处理它。要么使用异步库,要么使用请求的Promise变量。那么您正在尝试获取每个文章URL的正文?@ionizer是的,我正在尝试获取每个文章URL的正文。您知道如何解决此问题吗?非常感谢您的回答!您的答案肯定会在articleUrlArray中循环,但它不会输出我想要的内容。我知道您已经创建了checkBody函数并输出bodyOne或bodyOne&bodyTwo。但我不明白。我附上了我得到的截图。你能告诉我出了什么问题吗?试着用这个代替报税表,好吗?{bodyOne}:{bodyOne,bodyTwo},我没有函数请求的完整实现,所以我在假设。这会给你带来物品。谢谢你的回复。我尝试了你的解决方案,但我得到一个错误,说,响应不是一个函数。你知道如何解决这个问题吗?我更新了getArticle和checkBody函数,让它们有自己的承诺,看看我的新答案,我不想这样做,而是保持原来的功能,但我真的需要知道完整的实现,告诉我这是否可行。我很高兴听到这个消息!
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)))
})