News API server with GNews integration, search functionality, and caching
Version 1.0.0
by alfasoon <alfasoon@gmail.com>
- Built a custom in-memory caching system (no Redis needed)
- Uses native fetch API instead of axios for HTTP requests
- Cache keys are generated from URL paths and query parameters
- Expired cache entries get cleaned up every 5 minutes automatically
- Focus on find endpoints - search by title, author, or keywords
- Comprehensive error handling with descriptive messages and timestamps
- TODO: Add rate limiting when I have more time
https://github.com/alfasoon/news-api
- Node.js
- Express.js
- JavaScript
- GNews API
- Custom In-Memory Cache
- Fetch API
- CORS
N.A
start : $ node server.js
dev : $ nodemon server.js
test : $ echo "Error: no test specified" && exit 1
https://github.com/alfasoon/news-api/issues
ISC
- express : $ ^4.18.2
- cors : $ ^2.8.5
- nodemon : $ ^3.0.1
<img src="https://cdn.dribbble.com/users/2401141/screenshots/5487982/developers-gif-showcase.gif"
- README.md - Complete API documentation with examples
- server.js - Main server implementation with all routes
- services/gnewsService.js - GNews API integration
- services/cacheService.js - Custom caching system implementation
- controllers/ - Controller files for different API endpoints
- homeController.js - Home and health check endpoints
- newsController.js - News search and find operations
- routes/index.js - Route definitions and middleware setup
- middleware/cacheMiddleware.js - Cache middleware for API routes
- ```
- http://localhost:3000
- ```
- This API focuses on find endpoints rather than complex advanced search. It provides:
-
- Basic Search: Simple news search with essential parameters
-
- Find Endpoints: Specialized search by title, author, or keywords
-
- Cache Management: Built-in caching system for performance
-
- Health Monitoring: Health check endpoint
- Note: The find endpoints tend to work better than basic search in most cases.
- | Method | Endpoint | Description | Parameters |
- |--------|----------|-------------|------------|
- | GET | `/api/search` | Search news articles | `q={query}`, `lang={language}`, `country={country}`, `max={limit}` |
- | GET | `/api/find/title` | Find articles by title | `title={title_text}` |
- | GET | `/api/find/author` | Find articles by author | `author={author_name}` |
- | GET | `/api/find/keywords` | Find articles by keywords | `keywords={keyword1,keyword2}` |
- | GET | `/health` | Health check endpoint | None |
- Here are some working examples:
- ```bash
- curl "http://localhost:3000/api/search?q=technology&lang=en&country=us&max=5"
- curl "http://localhost:3000/api/find/title?title=artificial intelligence"
- curl "http://localhost:3000/api/find/author?author=Reuters"
- curl "http://localhost:3000/api/find/keywords?keywords=climate,environment"
- curl "http://localhost:3000/health"
- ```
- Note: All query parameters are replaceable with your desired values.
- Here are some country-specific examples:
- ```bash
- curl "http://localhost:3000/api/find/title?title=India economy"
- curl "http://localhost:3000/api/find/author?author=Times of India"
- curl "http://localhost:3000/api/find/keywords?keywords=india,politics,modi"
- curl "http://localhost:3000/api/search?q=india&country=in&lang=en&max=10"
- ```
- ```bash
- curl "http://localhost:3000/api/find/title?title=China technology"
- curl "http://localhost:3000/api/find/author?author=China Daily"
- curl "http://localhost:3000/api/find/keywords?keywords=china,economy,beijing"
- curl "http://localhost:3000/api/search?q=china&country=cn&lang=en&max=10"
- ```
- ```bash
- curl "http://localhost:3000/api/find/title?title=America politics"
- curl "http://localhost:3000/api/find/author?author=CNN"
- curl "http://localhost:3000/api/find/keywords?keywords=america,technology,california"
- curl "http://localhost:3000/api/search?q=america&country=us&lang=en&max=10"
- ```
- Note: The keywords endpoint typically returns more results than title/author searches.
- The API supports multiple languages:
- | Language | Code | Language | Code |
- |----------|------|----------|------|
- | English | `en` | Spanish | `es` |
- | French | `fr` | German | `de` |
- | Chinese | `zh` | Japanese | `ja` |
- | Arabic | `ar` | Russian | `ru` |
- | Portuguese | `pt` | Italian | `it` |
- | Dutch | `nl` | Swedish | `sv` |
- | Hindi | `hi` | Tamil | `ta` |
- | And more... | | | |
- Supported countries include:
- | Country | Code | Country | Code |
- |---------|------|---------|------|
- | United States | `us` | United Kingdom | `gb` |
- | Canada | `ca` | Australia | `au` |
- | Germany | `de` | France | `fr` |
- | Japan | `jp` | China | `cn` |
- | India | `in` | Brazil | `br` |
- | Spain | `es` | Italy | `it` |
- | And more... | | | |
- Note: Tested with `us`, `in`, and `cn` - other countries should work as well.
Built with โค๏ธ using Node.js and Express.js