Build a searchable news aggregator in ~200 lines of Node.js
A news aggregator collects articles from many publishers and shows them in one place. Building one yourself usually means finding feeds, parsing them, removing duplicates and tagging topics. With a news API, all of that becomes a single request. This tutorial builds a small, searchable news site in two files, using Node.js 18 or later and no dependencies . By the end you'll have: a grid of top…
Building a news aggregator in around 200 lines of Node.js code is straightforward when using a news API like DynamiRSS. This tutorial provides step-by-step instructions to create a simple, searchable news site using only two files in Node.js 18 or later, without any additional dependencies.
To begin, you'll need to obtain an API key by signing up for DynamiRSS. The free plan offers 500 requests per month, with up to 20 articles per request, which should be sufficient for this tutorial. Once you have your API key, make a test request to get familiar with the API response format. Using cURL, you can send a POST request to the DynamiRSS scan endpoint, specifying parameters such as category, image presence, and the number of results to retrieve.
Next, you'll want to keep the API key on a small server to prevent unauthorized access and excessive API usage. Create a folder containing a file named server.mjs. In this file, import the necessary modules from Node.js, including the http module and fs/promises for file reading. Define constants for the API URL, API key, port number, and cache duration in milliseconds.
The cache is implemented using a Map object, which stores the cached API responses along with their timestamps. The searchNews function acts as an intermediary between the browser and the news API. It receives parameters like query, category, and offset, builds the request body, and checks the cache before making a request to the news API. If the response is successful, the data is cached for future use. If the cache size exceeds 500 entries, the cache is cleared to prevent it from growing too large.
The HTTP server is set up using createServer, which listens for incoming requests on the specified port. When a request is received, the server responds with the cached news data or, if unavailable, a fresh request to the news API. The API key remains safely on the server, while the browser only communicates with your own server, ensuring the API key is protected and your quota is respected.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.