簡體   English   中英

Node.js&Express:如何通過“ for循環”使用Express.js創建許多app.get調用?

[英]Node.js & Express: How to create many app.get calls with Express.js through “for loop”?

在我的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的正文內容。

無論發生什么情況,我總是“僅”獲取最后一個URL的正文內容。 它沒有獲取“ articleUrlArray”中每個URL的正文內容。

我究竟做錯了什么?

這是下面運行Hugo Nasciutti解決方案后得到的屏幕截圖: 在此處輸入圖片說明

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)))
})

真正發生的是,我正在使用一個函數來帶來一個Promises數組,當所有的Promises都解決后,然后使用已字符串化的對象數組來響應請求。 我自由地實現了箭頭函數和常量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM