A practice project: a current weather and five-day forecast page in vanilla JavaScript, using the OpenWeatherMap API. The page text is German.
On load, the page asks the browser for the device location. If the browser refuses, does not support geolocation, or does not answer within 8 seconds, the page uses Vienna (48.208174, 16.373819).
With the coordinates, the page makes three requests to OpenWeatherMap:
/data/2.5/weatherfor the current conditions./data/2.5/forecastfor the 3-hour forecast over five days./geo/1.0/reverseto turn the coordinates into a place name. If this request fails, the page shows "Unbekannte Position".
The current section shows the temperature, the feels-like temperature, cloud cover, humidity, wind speed and direction, and the sunrise and sunset times. Icons come from openweathermap.org/img/wn/.
The forecast table has one column per later local calendar day. Each column uses the 3-hour entry closest to 12:00 local time for its icon, description and temperature. Today is skipped, and the first column is labelled "Morgen" (tomorrow).
The page loads its data once. It does not refresh on a timer.
- HTML, CSS and JavaScript in
index.html,style.cssandscript.js. There is no framework and no bundler. - jQuery 4.0.0 for the AJAX calls (
$.getJSON,$.when). - Bootstrap 5.3.9 stylesheet.
- Node.js 22.23.2 or newer and npm. Node runs the build, validation and test scripts. The app itself runs in the browser.
index.html loads ./assets/, which exists only in the build output. Open the built dist/ folder, not the repository root.
- Install the packages:
npm install - Set the API key (see below).
- Build:
npm run build - Serve
dist/with any static file server, for examplenpx --yes serve dist.
Get a key from OpenWeatherMap. The key is read from the OPENWEATHER_API_KEY variable when npm run build runs.
- Put it in a
.envfile. Copy.env.exampleto.envand replace the placeholder.generate-config.jsreads.env, and its values override variables already set in the shell. - Or set
OPENWEATHER_API_KEYin the shell beforenpm run build.
generate-config.js writes config.js with the key as a plain string. The browser loads that file, so anyone who opens the page can read the key. Treat the key as public.
Without a key, config.js holds an empty value. The page shows an "API key not configured" alert and sends no requests.
config.js and .env are in .gitignore.
npm run buildrunsgenerate-config.js, checks that the installed Bootstrap and jQuery versions matchpackage.json, and copies the static files intodist/.npm run validatechecks the JavaScript syntax, that both browser libraries are pinned inpackage.jsonandpackage-lock.json, that the HTML loads local assets only, and that the forecast table IDs exist.npm testruns the Node test runner ontests/. The tests cover the location fallback, forecast grouping, API error handling and the build output.npm run devandnpm run setuponly rungenerate-config.js. The project has no development server.
The live site runs on a self-hosted Coolify server. The build runs npm ci and npm run build with OPENWEATHER_API_KEY set as a build variable, and nginx serves dist/.
.github/workflows/pages.yml also publishes the build to GitHub Pages on pushes to main, at https://t-scheiber.github.io/AK_WeatherApp/. It runs only when the repository variable STATIC_PAGES_ENABLED is true. The workflow passes no secrets, so that copy's config.js contains an empty key. It shows the "API key not configured" alert and no weather data.
nixpacks.toml describes a third setup that serves the repository root with serve. The root has no assets/ folder, so serve dist/ instead.
index.html: page markup and the element IDs the script fills.script.js: location lookup, API requests and rendering.style.css: layout. The forecast table scrolls sideways on narrow screens.generate-config.js: writesconfig.jsfrom the API key.scripts/build.mjsandscripts/validate.mjs: build and validation.tests/: Node tests for the script, the config generator and the build.images/: favicons, touch icons and the web app manifest.