Last active
May 28, 2018 18:40
-
-
Save maisonm/f5340910ce17f527bf35c904063b0f8c to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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