A lightweight Node.js wrapper around the GNews API with built-in caching using node-cache. Provides endpoints to fetch top headlines and search for news articles with optional filters.
- In-memory caching for faster repeated responses (5 min TTL)
- Supports language, category, country, and max article filters
- Search endpoint for keyword-based article lookup
- Swagger documentation available at
/docs
- Node.js
- Express
- Axios
- node-cache
- Swagger UI
git clone https://github.com/ibrahnerd7/news-api.git
cd news-api
npm installCreate a .env file in the project root:
Head over to GNews API, create an account, grab the api key and paste it in the .env file
GNEWS_API_KEY=your_gnews_api_key_herenpm run startThe server will run on:
http://localhost:3000
Visit:
http://localhost:3000/docs
For full Swagger-based interactive API docs.
Fetch top headlines.
Query Parameters:
| Param | Type | Required | Description |
|---|---|---|---|
n |
integer | yes | Number of articles to return |
category |
string | no | News category (e.g. technology) |
lang |
string | no | Language code (default: en) |
country |
string | no | Country code (default: us) |
Example:
GET /articles?n=5&category=technology&country=usSearch articles by keyword.
Query Parameters:
| Param | Type | Required | Description |
|---|---|---|---|
q |
string | yes | Search term (e.g. bitcoin) |
Example:
GET /search?q=bitcoinnpm testUnit tests cover:
- Caching behavior
- Axios integration
- API error handling
MIT