This is a simple API that interacts with the GNews API for fetching news articles. Features:
- Node.js/Javascript server that uses
express
to serve pages,axios
to fetch data from GNews API andnode-cache
to cache requests - It parses errors from the GNews API and displays them to user along with 500 HTTP status code
- RESTful API (see docs on the endpoints below)
- API Docs
- Basic unit testing with Jest
- Github workflow integration with unit tests for commit validation
To install the project dependencies, run the following command:
npm install
To start the API server, run the following command:
npm start
This will start the server on port 3000 by default. You need to provide an API key in the API_KEY
constant in the code. A sample key is included.
To run the Jest test suite, run the following command:
npm test
This will run the tests and output the results to the console.
Note: The tests use the supertest library to test the API endpoints. The tests assume that the API server is running on port 3000. If you're running the server on a different port, you may need to modify the tests accordingly.
API service for fetching news articles from the GNews API
Base URLs:
- API Key (apiKey)
- Parameter Name: API_KEY, in: code.
let articles = await fetch('http://localhost:3000/articles?n=2'),
console.log(articles);
Name | In | Type | Required | Description |
---|---|---|---|---|
n | query | integer | yes | The number of articles to fetch (maximum 100) |
[
{
"title": "Article Title",
"description": "Article description.",
"url": "https://example.com/article",
"image": "https://example.com/article.jpg",
"publishedAt": "2022-05-10T12:34:56Z",
"source": {
"name": "Example News",
"url": "https://example.com"
}
},
{
"title": "Another Article",
"description": "Another article description.",
"url": "https://example.com/another-article",
"image": "https://example.com/another-article.jpg",
"publishedAt": "2022-05-09T09:12:34Z",
"source": {
"name": "Example News",
"url": "https://example.com"
}
}
]
Status | Meaning | Description | Schema |
---|---|---|---|
200 | OK | OK | Inline |
400 | Bad Request | Invalid request parameters | None |
500 | Internal Server Error | Internal server error | None |
let articles = await fetch('http://localhost:3000/articles/search?q=Cardano%20Reaches%20Another%20High');
Name | In | Type | Required | Description |
---|---|---|---|---|
q | query | string | yes | The search query. |
[
{
"title": "Cardano News Article",
"description": "Article description.",
"url": "https://example.com/article",
"image": "https://example.com/article.jpg",
"publishedAt": "2022-05-10T12:34:56Z",
"source": {
"name": "Example News",
"url": "https://example.com"
}
},
{
"title": "Another Cardano Article",
"description": "Another article description.",
"url": "https://example.com/another-article",
"image": "https://example.com/another-article.jpg",
"publishedAt": "2022-05-09T09:12:34Z",
"source": {
"name": "Example News",
"url": "https://example.com"
}
}
]
Status | Meaning | Description | Schema |
---|---|---|---|
200 | OK | OK | Inline |
400 | Bad Request | Invalid request parameters | None |
500 | Internal Server Error | Internal server error | None |
let articles = await fetch('http://localhost:3000/articles/Bitcoin'),
Name | In | Type | Required | Description |
---|---|---|---|---|
title | query | string | yes | The title of the article to retrieve. |
[
{
"title": "Bitcoin",
"description": "Article description.",
"url": "https://example.com/article",
"image": "https://example.com/article.jpg",
"publishedAt": "2022-05-10T12:34:56Z",
"source": {
"name": "Example News",
"url": "https://example.com"
}
}
]
Status | Meaning | Description | Schema |
---|---|---|---|
200 | OK | OK | Inline |
400 | Bad Request | Invalid request parameters | None |
500 | Internal Server Error | Internal server error | None |
{
"title": "string",
"description": "string",
"url": "string",
"image": "string",
"publishedAt": "2019-08-24T14:15:22Z",
"source": {
"name": "string",
"url": "string"
}
}
Name | Type | Description |
---|---|---|
title | string | The title of the article |
description | string | A brief description of the article |
url | string | The URL of the article |
image | string | The URL of the article's image |
publishedAt | string(date-time) | The date and time the article was published |
source | object | source of the article |
» name | string | The name of the news source |
» url | string | The URL of the news source |
The News API service caches the results of each API request for 1 hour to improve performance. If the same request is made within 1 hour, the cached results will be returned instead of making a new request to the GNews API.