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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.env
/node_modules
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Node.js Weather CLI Application

A simple command-line tool built with **Node.js** that fetches **real time weather data** from the **OpenWeather API** and displays it in your terminal.

Perfect for learning **async/await**, **API integration**, and **error handling** in Node.js.

---

## Features

- Fetch weather by city name
- Async/Await API calls
- Environment variables for API key security
- Error handling for:
- Invalid city
- Invalid API key
- Network issues
- Clean and readable ES6+ code structure

---

## Installation

1) Clone the repository:

```
git clone <your-repo-url>
cd weather-cli
```

2) Install dependencies:
```
npm install
```

## Setup Environment Variables

Create a .env file in the project root:

```
OPENWEATHER_API_KEY=your_real_api_key_here
```

## Usage

Run the CLI with a city name:
node index.js Algiers

### Example output:
```
City: Algiers
Temperature: 12.68°C
Description: mist
humidity: 53%
```
## Error Handling

The app gracefully handles common errors:
| Error Type | Message Example |
| --------------- | --------------------------------------- |
| Invalid city | city not found, check the city name and try again|
| Invalid API key | invalid API key |
| Network issue | network error, check your internet connection |
| Missing API key | API key missing in .env file |

## Technologies Used

- Node.js – Runtime environment
- Axios – HTTP requests library
- dotenv – Environment variable management
- OpenWeather API – Real-time weather data

## 📁 Project Structure
```
weather-cli/
├── index.js # Main CLI logic
├── package.json # Node.js dependencies
├── .env # API key (ignored in Git)
├── .gitignore # Exclude node_modules and .env
└── README.md # Project documentation
```

### Made by [Maya Otsmane]
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//CLI (for testing / running manually)


//Sends a request to a weather API
//Gets data
//Returns useful info

//loading environment variables : 1)Reads .env file, 2)Loads API key into process.env
require("dotenv").config();
//axios is a library to send http requests
const axios = require("axios");
//creating the tool function
async function weatherTool(city) {
try {
//async because API calls take time (internet), async allows await.
const key = process.env.OPENWEATHER_API_KEY; //it reads the api key from .env
if (!key) {
throw new Error("API key is missing in .env file");
}
//creating the api url
const url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid=${key}`;
//n3ayto lel api
const res = await axios.get(url); //This sends an HTTP GET request. equivalent to opening the url in a browser

return { //extracting useful data
city: res.data.name,
temp: res.data.main.temp,
description: res.data.weather[0].description,
humidity: res.data.main.humidity
};
} catch (error) {
//case if api responded with an error
if (error.response) {
if (error.response.status === 404) { //city not found
console.log("city not found, check the city name and try again");
} else if (error.response.status === 401) { //invalid api key
console.log("invalid API key");
} else { //other problems
console.log("API error:", error.response.data.message);
}
} else if (error.request) {
console.log("network error, check your internet connection");
} else {
console.log("ERROR", error.message);
}

return null;
}
}

//testing
(async () => {
const city = process.argv[2] || "Algiers";
const data = await weatherTool(city);
if (data) {
console.log("City:", data.city);
console.log("Temperature:", data.temp + "°C");
console.log("Description:", data.description);
console.log("humidity:", data.humidity + "%");
}
})();

//kmlna the weather CLI core
//3yit :)
Loading