The Weather App is a JavaScript-based web application that uses the OpenWeatherMap API to fetch and display weather data for any city. It shows the current weather conditions, an hourly forecast, and a 7-day forecast. The app also includes geolocation functionality to show weather based on the user's current location.
This file handles all the core logic of the application, including fetching data from the OpenWeatherMap API and displaying it on the screen.
-
gps()- Purpose: Uses the browser's Geolocation API to retrieve the user's current latitude and longitude.
- Details: This function checks if geolocation is available in the browser. If it is, it retrieves the coordinates and calls
fetchForecastData(lat, lon)to get weather data based on the location.
-
makeRequest(city)- Purpose: Fetches weather data for a specific city.
- Details: This function takes the city name as a parameter, constructs a request URL using the OpenWeatherMap API, and calls
fetchData()to retrieve weather data.
-
fetchData(city)- Purpose: Retrieves current weather data for the provided city.
- Details: It uses the OpenWeatherMap API’s
weatherendpoint to get real-time weather information like temperature, weather conditions, and location data. The fetched data is then passed todisplayWeather().
-
fetchForecastData(lat, lon)- Purpose: Fetches the 7-day weather forecast based on latitude and longitude.
- Details: This function uses the OpenWeatherMap API’s
forecastendpoint to retrieve the weather forecast for the next 7 days. It takes the latitude and longitude as parameters and fetches the data.
-
displayWeather(cityData, forecastData)- Purpose: Updates the DOM with the weather information.
- Details: This function processes the fetched weather data and populates the webpage with the current weather, hourly forecast, and 7-day forecast. It uses the JSON response to extract relevant weather details such as temperature, humidity, wind speed, and weather icons.
-
formatDateTime()- Purpose: Formats the time and date for display on the webpage.
- Details: Converts Unix timestamps to human-readable dates and times, which are used in displaying forecast times and dates in the app.
This file stores the OpenWeatherMap API key, which is used for making API requests.
API_KEY- Purpose: Holds the OpenWeatherMap API key.
- Details: Replace
'your_api_key'with the actual API key from OpenWeatherMap to enable API requests.
This file defines the styling of the application using Tailwind CSS. Tailwind is a utility-first CSS framework that makes it easy to design responsive and modern web applications.
container: Defines the main layout container for the app.text-center: Centers the text inside weather elements like city name, temperature, and weather conditions.grid: Used for creating the grid layout for displaying the hourly and 7-day forecast.bg-gray-200: Adds a light gray background to various elements.- Responsive classes: Includes Tailwind’s responsive design utilities like
sm:text-sm,md:text-lgto ensure proper display on different screen sizes.
This file configures Webpack, a tool that bundles JavaScript and CSS files for the application.
-
entry
- Purpose: Defines the entry point of the application.
- Details: The
src/index.jsfile is specified as the entry point, meaning Webpack will begin bundling from this file.
-
output
- Purpose: Defines where the bundled files will be saved.
- Details: The bundled files will be outputted to the
dist/folder asbundle.js.
-
module
- Purpose: Specifies rules for processing different file types.
- Details: Uses the
css-loaderandpostcss-loaderto process CSS files, including Tailwind CSS.
This file configures PostCSS, a tool that transforms CSS files with JavaScript plugins. It processes the CSS to ensure browser compatibility and includes Tailwind CSS.
tailwindcss: Enables Tailwind CSS utility classes in the project.autoprefixer: Automatically adds browser-specific prefixes to CSS properties, ensuring compatibility across different browsers.
This file customizes the Tailwind CSS framework for the project. It defines which parts of the project should be scanned for class names and customizes the theme used in the app.
-
content
- Purpose: Defines the files where Tailwind should look for class names.
- Details: The
src/directory is scanned by Tailwind, ensuring that only the necessary CSS classes are included in the final build.
-
theme
- Purpose: Extends the default Tailwind theme.
- Details: Allows for customization of colors, spacing, and other design elements.
This file configures ESLint, a static analysis tool that helps enforce coding standards and identifies potential errors in the JavaScript code.
quotes: Enforces single quotes for strings in JavaScript.no-unused-vars: Prevents unused variables in the code.no-console: Allowsconsole.logfor debugging purposes during development but can be disabled for production.
This file contains metadata about the project and manages its dependencies and scripts.
-
dependencies: Lists the external libraries and packages the project depends on:
- Tailwind CSS: Utility-first CSS framework.
- Webpack: Module bundler for JavaScript and CSS.
- ESLint: Linting tool to ensure code quality.
-
scripts: Defines commands for building and running the project:
build: Runs Webpack to bundle the project.watch: Watches for changes in files and automatically rebuilds the project when changes are detected.
- Geolocation Support: Automatically fetches and displays weather data based on the user’s current location.
- City Search: Allows users to search for and display weather information for any city in the world.
- 7-Day Forecast: Displays a detailed 7-day weather forecast for the selected city or location.
- Responsive Design: The app uses Tailwind CSS to provide a mobile-friendly, responsive design.
-
Clone the Repository:
git clone <repository-url>
-
Install Dependencies: Navigate to the project directory and run:
npm install
-
Add OpenWeatherMap API Key: In the file
key.js, replace'your_api_key'with your actual API key from OpenWeatherMap. -
Build and Serve the Application: Build the project using Webpack:
npm run build npm run watch
-
Open the App: Open the index.html file in your browser to view the weather app.
