Objective: Build a RESTful API that allows users to fetch news articles from multiple sources based on their preferences.
Project Description: In this project, we will create a RESTful API using Node.js, Express.js, and NPM packages. The API will allow users to register, log in, and set their news preferences (e.g., categories, sources). The API will then fetch news articles from multiple sources using external news APIs (e.g., NewsAPI). The fetched articles should be processed and filtered asynchronously based on user preferences.
git clone <repository_url>
npm install
Audit installed dependencies using below command (fix the dependencies if any vulnerabilities found)
npm audit
PORT=Port number where server will run at
MONGODB_CONNECTION=MongoDB connection string
JWT_SECRET=JWT secret to sign/decode JWT
NEWS_API_KEY=News API key configured on your account. Can be found at https://newsapi.org/
npm run start
http://localhost:PORT/tasks
NOTE
Test the API using Postman. Collections are available in public folder for reference
npm run test:unit
POST /register
curl --location 'http://localhost:8996/user/register' \
--header 'Content-Type: application/json' \
--data-raw '{
"email": "[email protected]",
"firstName": "Test",
"lastName": "Testing",
"password": "Test@1234",
"role": "user"
}'
Status 201
POST /login
curl --location 'http://localhost:8996/user/login' \
--header 'Content-Type: application/json' \
--data-raw '{
"email": "[email protected]",
"password": "Test@1234"
}'
Status 200 with access token
PUT /preferences
curl --location --request PUT 'http://localhost:8996/preferences' \
--header 'Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjY1MjUyYTQ4MjBmNzJmM2VhMmI2MjEyNCIsInJvbGUiOiJ1c2VyIiwic3ViIjoidGVzdEBnbWFpbC5jb20iLCJpYXQiOjE2OTY5MzcwMjYsImV4cCI6MTY5NzAyMzQyNn0.rGFzaZ7S_kI0kmyraIFtyptU494XpnWFIhWZnqwBLZ0' \
--header 'Content-Type: application/json' \
--data '{
"category": "technology"
}'
category
token in authorization header
Available categories are: business, entertainment, general, health, science, sports, technology.
Default is business
Status 200
GET /preferences
curl --location 'http://localhost:8996/preferences' \
--header 'Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjY1MjUyYTQ4MjBmNzJmM2VhMmI2MjEyNCIsInJvbGUiOiJ1c2VyIiwic3ViIjoidGVzdEBnbWFpbC5jb20iLCJpYXQiOjE2OTY5MzcwMjYsImV4cCI6MTY5NzAyMzQyNn0.rGFzaZ7S_kI0kmyraIFtyptU494XpnWFIhWZnqwBLZ0'
token in authorization header
Status 200 with user preferences, if any set by user.
GET /news
curl --location 'http://localhost:8996/news' \
--header 'Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjY1MjUyYTQ4MjBmNzJmM2VhMmI2MjEyNCIsInJvbGUiOiJ1c2VyIiwic3ViIjoidGVzdEBnbWFpbC5jb20iLCJpYXQiOjE2OTY5MzcwMjYsImV4cCI6MTY5NzAyMzQyNn0.rGFzaZ7S_kI0kmyraIFtyptU494XpnWFIhWZnqwBLZ0'
token in authorization header
Status 200 with news articles based on the logged-in user's preferred category.
Default is business
GET /news/search/:keyword
curl --location 'http://localhost:8996/news/search/bitcoin' \
--header 'Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjY1MjUyYTQ4MjBmNzJmM2VhMmI2MjEyNCIsInJvbGUiOiJ1c2VyIiwic3ViIjoidGVzdEBnbWFpbC5jb20iLCJpYXQiOjE2OTY5MzcwMjYsImV4cCI6MTY5NzAyMzQyNn0.rGFzaZ7S_kI0kmyraIFtyptU494XpnWFIhWZnqwBLZ0'
token in authorization header
Status 200 with news articles for searched keyword