A comprehensive NestJS-based weather forecast API that provides current weather data and forecasts for cities worldwide using the OpenWeatherMap API.
- Real-time current weather data
- 5-day weather forecasts with 3-hour intervals
- City search tracking and statistics
- Scheduled background data refresh
- SQLite database with TypeORM
- Comprehensive API documentation with Swagger
- Input validation and error handling
- CORS enabled for cross-origin requests
- Framework: NestJS with TypeScript
- Database: SQLite with TypeORM
- External API: OpenWeatherMap API
- Documentation: Swagger/OpenAPI
- Validation: class-validator
- Scheduling: @nestjs/schedule
- HTTP Client: Axios
- Node.js (v16 or higher)
- npm or yarn
- OpenWeatherMap API key
- Clone the repository
git clone <repository-url>
cd weather_forecast_service- Install dependencies
npm install- Set up environment variables
Create a
.envfile in the root directory:
OPENWEATHER_API_KEY=your_openweathermap_api_key_here
PORT=3000
- Build the application
npm run build- Start the application
npm startThe server will start on http://localhost:3000
GET /api/weather/current?city={cityName}- Get current weather for a cityGET /api/weather/forecast?city={cityName}- Get 5-day weather forecastGET /api/weather/history- Get search historyGET /api/weather/cities- Get all tracked cities
GET /- Main dashboard pagePOST /search- Search functionality
Interactive API documentation is available at:
http://localhost:3000/api/docs
The application uses four main entities:
- Stores city information with coordinates
- Tracks search statistics
- Primary reference for weather data
- Current weather data for cities
- Linked to CityEntity via foreign key
- Automatically updated on API calls
- Weather forecast data points
- 3-hour interval forecasts up to 5 days
- Linked to CityEntity
- Tracks search patterns
- Records search frequency and timestamps
- Current Weather: Cached for 15 minutes
- Forecast Data: Cached for 1 hour
- Background Refresh: Scheduled hourly updates for popular cities
The API provides comprehensive error responses:
200 OK- Success400 Bad Request- Invalid input or API key issues404 Not Found- City not found500 Internal Server Error- Server errors
npm start- Start production servernpm run start:dev- Start development server with hot reloadnpm run build- Build the applicationnpm run test- Run testsnpm run lint- Run ESLint
src/
├── controllers/ # API controllers
├── services/ # Business logic services
├── entities/ # Database entities
├── dto/ # Data transfer objects
├── app.module.ts # Main application module
└── main.ts # Application entry point
OPENWEATHER_API_KEY- Required: Your OpenWeatherMap API keyPORT- Optional: Server port (default: 3000)
The application uses SQLite by default. The database file is automatically created as weather.db in the project root.
- Database queries are optimized to prevent N+1 query problems
- API responses are cached to reduce external API calls
- Background tasks refresh popular city data proactively
- Proper indexing on frequently queried fields
- Input validation on all endpoints
- Environment variable protection for API keys
- CORS configuration for secure cross-origin access
- Error messages sanitized to prevent information leakage
- Morgan middleware for HTTP request logging
- Structured logging for debugging and monitoring
- Error tracking with detailed stack traces in development
This project is licensed under the MIT License.
- Visit the NestJS Documentation to learn more about the framework.
- For questions and support, please visit our Discord channel.
- To dive deeper and get more hands-on experience, check out our official video courses.
- Deploy your application to AWS with the help of NestJS Mau in just a few clicks.
- Visualize your application graph and interact with the NestJS application in real-time using NestJS Devtools.
- Need help with your project (part-time to full-time)? Check out our official enterprise support.
- To stay in the loop and get updates, follow us on X and LinkedIn.
- Looking for a job, or have a job to offer? Check out our official Jobs board.
Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.
- Author - Kamil Myśliwiec
- Website - https://nestjs.com
- Twitter - @nestframework
Nest is MIT licensed.