Created
June 8, 2017 18:00
-
-
Save joshgav/e2205d03d7fc357b2d3c7b28dffc71d8 to your computer and use it in GitHub Desktop.
Simple Weather API (sample)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const http = require('http'); | |
const url = require('url'); | |
// assumes a sibling .env file with | |
// OPENWEATHER_APIKEY=your_key | |
const dotenv = require('dotenv').config(); | |
const appid = process.env.OPENWEATHER_APIKEY; | |
function httpHandler (request, response) { | |
let parsed_url = url.parse(request.url, true); | |
let zip = parsed_url.query.zip; | |
let country = parsed_url.query.country || 'us'; | |
let query_string = `zip=${zip},${country}&APPID=${appid}` | |
http.get(`http://api.openweathermap.org/data/2.5/forecast?${query_string}`, | |
weather_response => { weather_response.pipe(response, {end: true}) } | |
); | |
} | |
http.createServer(httpHandler).listen(8080); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment