This project is a RESTful API built using Node.js and Express.js that allows users to fetch news articles based on their preferences. It includes user registration, login, and the ability to set and update news preferences. Additionally, it also includes getting news article details, users can mark the article as read and favorite, retrieve all read and favorite articles. The API integrates with the World News API for fetching news articles. All network calls to this external API is cached for faster response times. All cache keys are updated in background at regular intervals.
-
Clone the repository:
git clone https://github.com/adnan-sheikh/news-aggregator-api.git
-
Install dependencies:
cd news-aggregator-api npm install
-
Set up environment variables:
Create a
.env
file in the project root and add the following:PORT=3000 NEWS_API_URL_V2="https://api.worldnewsapi.com" NEWS_API_KEY_V2="your_api_key" JWT_AUTH_KEY="a_super_secret_key"
Replace both
NEWS_API_KEY_V2
andJWT_AUTH_KEY
with your keys -
Run the application:
npm start
The API will be accessible at
http://localhost:3000
for registration and login, andhttp://localhost:3000/api/v1
for other endpoints.
Endpoint: POST /register
Request Body:
{
"username": "example_user",
"password": "securepassword"
}
Response body:
{
"id": "4da53050-8785-4266-99ae-bbb259d628f5",
"username": "example_user",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVC"
}
This token
needs to be saved by the client, for further access to different endpoints.
Endpoint: POST /login
Login have same request and response bodies.
Endpoint: POST /api/v1/preferences
Requires authentication.
Request Body:
{
"source-countries": "us",
"language": "en",
"authors": "John Doe"
}
Note: source-countries
and language
are required. authors
is also optional and can be either single value or comma-separated multiple values. Even multiple source-countries can be specified as comma-separated values.
Example:
{
"source-countries": "us,ca,gb",
"language": "en"
}
Note: source-countries
must follow ISO 3166, and language
must follow ISO 6391.
Endpoint: GET /api/v1/preferences
Requires authentication.
Endpoint: PUT /api/v1/preferences
Requires authentication.
Request and Response body same as POST /api/v1/preferences
Endpoint: GET /api/v1/news
Requires authentication.
Endpoint: GET /api/v1/news/:id
Requires authentication.
Endpoint: POST /api/v1/news/:id/read
Requires authentication.
Endpoint: POST /api/v1/news/:id/favorite
Requires authentication.
Endpoint: GET /api/v1/news/read
Requires authentication.
Endpoint: GET /api/v1/news/favorites
Requires authentication.
Endpoint: GET /api/v1/news/search/:keyword
Requires authentication.
The API implements in-memory caching for external API calls to reduce the load on the World News API.
Upon successful registration, the user is automatically logged in, i.e the token is sent after registration too, because we're not doing any multi-factor auth as of now.
Errors are logged to a main.error.log
file under /src/logs
directory for monitoring and debugging.
Error responses to the client are sent in the form of:
{
"error": "Something went wrong"
}
When validating inputs, muliple errors are sent to the client in the form of:
[
{
"location": "source-countries",
"message": "this field cannot be empty!"
},
{
"location": "language",
"message": "Invalid language. It must follow ISO 6391"
}
]
The server updates the cache in the background at regular intervals to simulate real-time updates.