Skip to content

Instantly share code, notes, and snippets.

@maisonm
Last active May 28, 2018 18:40
Show Gist options
  • Save maisonm/f5340910ce17f527bf35c904063b0f8c to your computer and use it in GitHub Desktop.
Save maisonm/f5340910ce17f527bf35c904063b0f8c to your computer and use it in GitHub Desktop.
const fetch = require('node-fetch');
module.exports = (app) => {
let zipcode;
app.post('/search-location', (req, res) => {
zipcode = req.body.zipcode;
if(!zipcode || zipcode.length < 5 || zipcode.length > 5) {
res.redirect('/error');
} else {
res.redirect('/current-weather');
}
})
app.get('/search-location-weather', (req, res) => {
//build api URL with user zip
const baseUrl = 'http://api.openweathermap.org/data/2.5/weather?zip=';
const apiId = '&appid=<YOUR API KEY GOES HERE>&units=imperial';
const userLocation = (url1, url2, zipcode) => {
let newUrl = url1 + zipcode + url2;
return newUrl;
};
const apiUrl = userLocation(baseUrl, apiId, zipcode);
fetch(apiUrl)
.then(res => res.json())
.then(data => {
res.send({ data });
})
.catch(err => {
res.redirect('/error');
});
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment