Note: This will be a pair programming activity. After the previous lesson, how comfortable are you with completing the steps below? After a fist-to-five, find a similar number to pair up with. Choose one computer to work on, and a driver to start coding.
Today we will use Node.js to make a request to Weather Underground. Here are the steps to get set up. If you get stuck, remember to Read, Search, then Ask.
- Get an API key from Weather Underground.
- Set up your initial npm environment.
- Create
index.js
. - Place your API key in a separate
env.js
file.
If you already have an API key from the last execise, you can skip this step.
- Go to Weather Underground.
- Sign up for an account by clicking the "Join" link at the top right and following the steps.
- Wait a few seconds, then check your email for a Verification email of Weather Underground. You may need to check your spam or other folders.
- Click the "Validate Your Email" button.
- Return to Weather Underground
- Click the "More" dropdown and select "Weather API for Developers" near the bottom of the dropdown.
- Click the "Pricing" tab to "purchase" a free key and follow the steps. You may need to refresh the page if it claims you have not validated your email yet.
- Your API key will be displayed on the page.
Do the following inside your WDI work folder.
- Fork and clone this repo.
- Enter the
node-cli-weather
directory. - Run
npm install
to install all the dependencies.
- Require the
request
library so you can reach the weatherUnderground service. - Look at the Weather Underground API Documentation to find a way to get JSON weather data.
- Make a request to Weather Underground to find Denver weather data.
- Print this data to the console in the form "The weather in Denver is
<cloudy>
" or similar. - Test this to verify it works!
An env.js
file is a great way to keep your API secret. We'll add our key to a seperate file, then list that file in our .gitignore
, so that it's not pushed up to our remote repo.
- In your
env.js
file, turn your api key into a varible. - Export that variable.
What would that file look like?
'use strict'
const apikey = "12345678";
module.exports = apikey;