This project is a simple Express.js API for managing and searching articles. The API allows you to add articles, view all articles, search for specific articles by title or keyword, and fetch articles by ID.
- Add articles with a unique ID, title, and text.
- Retrieve all articles.
- Search for articles by title or keyword and sorts them based on keyword frequency.
- Get details of a specific article by its ID.
- Clone the repository.
- Install dependencies:
npm install
- Start the server:
node index.js
The server will run on http://localhost:3000.
Description: Returns a message indicating that the API is running.
Response:
"Article API is running"Description: Adds a new article to the list.
Request Body:
{
"id": "<unique_id>",
"title": "<article_title>",
"text": "<article_text>"
}Responses:
- Success:
{ "message": "success" } - Failure:
{ "message": "failed. Invalid article format" }
Description: Returns the list of all articles.
Response:
{
"articles": [
{
"id": "<id>",
"title": "<title>",
"text": "<text>"
},
...
]
}Description: Fetches a specific article by its ID.
Query Parameters:
id: The unique ID of the article.
Responses:
- Success:
{ "article": { "id": "<id>", "title": "<title>", "text": "<text>" } } - Failure:
{ "error": "article not found" }
Description: Searches for articles by title or keyword in the text.
Query Parameters:
title: The title of the article to search for.key: A keyword to search for in the article text.
Responses:
- Search by Title (Success):
{ "article": { "id": "<id>", "title": "<title>", "text": "<text>" } } - Search by Keyword (Success):
{ "articles": [ { "book": { "id": "<id>", "title": "<title>", "text": "<text>" }, "keyfreq": <frequency> }, ... ] } - Failure:
{ "message": "failed" }
Counts the frequency of a keyword (key) in a paragraph (para).
Sorts a list of articles by the frequency of the keyword in descending order.
- Add an article:
curl -X POST http://localhost:3000/articles \ -H "Content-Type: application/json" \ -d '{"id": "1", "title": "Hello World", "text": "hello hello world"}'
- View all articles:
curl http://localhost:3000/viewarticles
- Search for an article by title:
curl "http://localhost:3000/search?title=Hello%20World" - Search for an article by keyword:
curl "http://localhost:3000/search?key=hello"