I am beginning learning the basics of developing APIs and am following along with a video on YouTube by a developer named Ania Kubow. There are three javascript libraries in use which are ExpressJS, Cheerio & Axios. I have picked up what she is telling us and going well enough. I am however, getting an error when executing the code so far. Below here is the main part of the example API:
Note:The “app” variable is referring to Express JS
app.get('/news', (req, res) => {
axios.get('https://www.theguardian.com/environment/climate-crisis')
.then((response) => {
// Store the html retrieved via the axios http GET request
const html = response.data;
// Load the retrieved html into the cheerio instance and store to $ cheerio selector
const $ = cheerio.load(html);
// DEBUG purpose stuff
var loopCounter = 0;
var climate_A_Elements = $('a:contains(climate)', html).length;
console.log('Number of articles found: ' + climate_A_Elements);
//
// Get all articles that contain 'climate' in the <a /> element
var allFoundArticles = $('a:contains("climate")', html);
// Iterate through all found articles using cheerio forEach loop
allFoundArticles.each(function () {
// Assign article title and url it is located at to variables
var title = $(this).text();
var url = $(this).attr('href');
// Push the previously defined vars to the previously defined array
articlesData.push({
title,
url,
});
// Output the article details to page as response
res.json(articlesData);
// Add to loop count for debugging purposes and log in console
loopCounter += 1;
console.log('Loops: ' + loopCounter);
}).catch(err => {
console.log(err);
})
})
})
After completing one iteration of the Cheerio each loop, the application errors and gives the below error output:
node:internal/errors:484
ErrorCaptureStackTrace(err);
^
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at new NodeError (node:internal/errors:393:5)
at ServerResponse.setHeader (node:_http_outgoing:644:11)
at ServerResponse.header (C:UsersetommDownloadsDanielDevWebclimate-change-apinode_modulesexpresslibresponse.js:794:10)
at ServerResponse.json (C:UsersetommDownloadsDanielDevWebclimate-change-apinode_modulesexpresslibresponse.js:275:10)
at Element.<anonymous> (C:UsersetommDownloadsDanielDevWebclimate-change-apiindex.js:65:21)
at LoadedCheerio.each (C:UsersetommDownloadsDanielDevWebclimate-change-apinode_modulescheeriolibapitraversing.js:519:26)
at C:UsersetommDownloadsDanielDevWebclimate-change-apiindex.js:52:30
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
code: 'ERR_HTTP_HEADERS_SENT'
}
Node.js v18.12.1
[nodemon] app crashed - waiting for file changes before starting...
I have of course, left a question as a comment on the video mentioned above, but realistically I don’t expect to receive a response, so after trying to find the issue for a while decided to try here for some help and guidance.
Any guidance would be greatly appreciated. If you require anything to help further please ask.
Thankyou.
I tried changing to the version of node installed on the video mentioned to see if it was a version issue, but it didn’t solve anything. I tried refactoring the code and stepping through a few times with no results and tried searching the web but couldn’t find the answer to this particular issue.