I'm a developer who grew up in Houston, TX. I've done a lot of things in my life and have finally found that software and development is truly what makes me happy.
I've been a musician, a teacher, a flight attendant, and airline pilot, and now a full stack software engineer.
I get to learn about new tech every day. And I'm here to share it all with you.
I'm Richard - a self-taught developer that has been building things since 2016. It's crazy how fast things are changing and how many new frameworks show up out of nowhere every week.
What We’ll Build
This will be the beginning of a Kubernetes series that will guide us through deploying a simple Node / Express application to a Kubernetes cluster on DigitalOcean.
Let’s build a simple Express application that queries a REST API. Instead of querying an API directly from you client-side application, we’ll build a small proxy API to stand in the middle of your frontend application and your backend API. Let’s use the OpenWeatherMap API for this project.
Prerequisites
Beginner knowledge of REST APIs
Some knowledge of Express (We won’t go into too much detail on the creation of the Express API)
We’ll use the Express framework to build out our api. This framework will start a node server and allow us to create custom routes. Start out by creating a new directory for this project.
mkdir k8s-series && cd k8s-series
Express runs on a node server, so let’s initialize a new npm package for this project.
# yarn
yarn init -y
# npm
npm init -y
For this project, you need a few packages: express and dotenv, and nodemon.
Fetching the current weather for our zip code will require two calls to OpenWeather. The first is a call to the GeoLocation API which will turn our zip code into lat and lon. The second API call will fetch the current weather with the coordinates we receive.
Create a new file called index.js in the root of your project’s directory and add the following code.
// index.jsconst express = require('express');
require('dotenv').config();
const app = express();
const PORT = 3000;
app.get('/', (req, res) => {
res.send('Hello Weather');
});
// Get the weather for a zip code.
app.get('/weather/zip/:zip/:country?', async (req, res) => {
// Get the zip from the paramsconst { zip } = req.params;
// If we have a country in the params, use it, else use 'US'const country = req.params.country || 'US';
// Get the lat / lon for the ziplet latLonRes = await fetch(
`http://api.openweathermap.org/geo/1.0/zip?zip=${zip},${country}&appid=${process.env.WEATHER_API_KEY}`
);
const { lat, lon } = await latLonRes.json();
// Get the weather for the lat / lon received from the previous requestconst weatherRes = await fetch(
`https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&units=metric&appid=${process.env.WEATHER_API_KEY}`
);
const weatherJson = await weatherRes.json();
// Return a 200 response with our weather data as JSON
res.status(200).json({ data: weatherJson });
});
// Start the app on Port 3000
app.listen(PORT, () => {
console.log(`Example app listening on port ${PORT}`);
});
Start up your server with yarn start or npm run start. Head over to a browser and visit localhost:3000/weather/zip/90210. You should see the current weather for Beverly Hills as JSON in your browser window.
Next, we’ll look at containerizing this app with Docker.
I encourage you to play around with the API you just built. Check out the OpenWeather API docs and try to pass a city name instead of a zip code. If you’d like more detailed info, consider signing up for their 3.0 OneCall API. You get 1,000 calls a day for free, but you must give them your payment info, so be careful