Skip to content

Instantly share code, notes, and snippets.

@joshgav
Created June 8, 2017 18:00
Show Gist options
  • Save joshgav/e2205d03d7fc357b2d3c7b28dffc71d8 to your computer and use it in GitHub Desktop.
Save joshgav/e2205d03d7fc357b2d3c7b28dffc71d8 to your computer and use it in GitHub Desktop.
Simple Weather API (sample)
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