Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.