An API, or Application Programming Interface, is a set of rules and protocols that allows different software applications to communicate with each other. It defines the methods and data formats that applications can use to request and exchange information or functionality. APIs are essential for enabling the integration and interaction between different software systems, services, and platforms.
This is a Climate Change News API which extracts climate change news from Newspapers and provides that news to the clients.
Import necessary packages: The script starts by importing the required Node.js packages, including Express.js, Axios for making HTTP requests, and Cheerio for parsing HTML.
Define the port: The
PORT
constant is set to 8000, indicating that the server will listen on port 8000.Create an array of newspapers: An array named
newspapers
is defined, where each element is an object containing the name of a newspaper and its URL related to climate change news.Create an Express app: An Express.js application is created using
express()
.Set up a root route: A root route (
/
) is defined to return a simple JSON response as a welcome message when the API is accessed.Set up a
/news
route: A route named/news
is defined to scrape articles from the specified newspaper URLs. It uses asynchronous functions to fetch and parse the HTML content of each newspaper's page using Axios and Cheerio. It then extracts the article titles and URLs containing the word "climate" and adds them to anarticles
array. Finally, the API responds with a JSON array containing the extracted articles, along with their source newspaper.Error handling: Error handling is implemented to catch and log any errors that may occur during the scraping process
Start the server: The Express.js server is started on the specified port, and a message is logged to indicate that the server is running.