Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions web/Learning-Season/03-backend/challenges/solution/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.env
/node_modules
52 changes: 52 additions & 0 deletions web/Learning-Season/03-backend/challenges/solution/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# 🌦 Weather CLI App

A Node.js command-line application that fetches real-time weather data from the [OpenWeatherMap API](https://openweathermap.org/api) and displays it in the console.

---

## Features

- Interactive city input — no flags or arguments required
- Also supports CLI argument for scripted/automated use
- Clean error handling for invalid cities, bad API keys, and network failures
- Modular architecture: service layer, display layer, and entry point are fully separated
- Secure API key management via environment variables

---



## Example Output

```
Enter city name: Alger
Fetching weather data for Algiers...
--------------------
City: Algiers, DZ
Temperature: 14°C
Humidity: 48%
Wind Speed: 0.51 m/s
Description: fog
--------------------
```

---

## Error Handling

| Scenario | Behavior |
|---|---|
| Empty city input | Prints usage message and exits |
| City not found (404) | Prints `[404] city not found` |
| Invalid API key (401) | Prints `[401] Invalid API key` |
| No internet / network failure | Prints fetch error message |

---

## Tech Stack

- **Node.js** (ES Modules)
- **node-fetch** — HTTP requests
- **dotenv** — environment variable management
- **readline** — interactive terminal input

38 changes: 38 additions & 0 deletions web/Learning-Season/03-backend/challenges/solution/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//Reads cli args, calls the service, passes the result to display.
import dotenv from 'dotenv';
dotenv.config();
import readline from 'readline';
import {fetchWeather} from './services/weatherService.js';
import {displayWeather} from './utils/display.js';


async function askCity(){
const rl=readline.createInterface({
input: process.stdin,
output: process.stdout,
});
return new Promise((resolve)=>{
rl.question('Enter city name: ',(city)=>{
rl.close();
resolve(city.trim());
});
})
}
async function main() {
const city= process.argv[2]|| (await askCity());//argv[0]=node, argv[1]=script, argv[2]=first

if(!city){
console.error('Error: City name cant be empty');
process.exit(1);
}
console.log(`Fetching weather data for ${city}...`);
try{
const weather= await fetchWeather(city);
displayWeather(weather);
} catch(err){
console.error('Error:',err.message);
process.exit(1);
}
}

main();
Loading