Created
June 24, 2017 15:47
-
-
Save bmorelli25/c67347560fe7b2213fea0d32d66fa53f to your computer and use it in GitHub Desktop.
app.post()
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 request = require('request'); | |
| const apiKey = '*****************'; | |
| //... | |
| //... | |
| app.post('/', function (req, res) { | |
| let city = req.body.city; | |
| let url = `http://api.openweathermap.org/data/2.5/weather?q=${city}&units=imperial&appid=${apiKey}` | |
| request(url, function (err, response, body) { | |
| if(err){ | |
| res.render('index', {weather: null, error: 'Error, please try again'}); | |
| } else { | |
| let weather = JSON.parse(body) | |
| if(weather.main == undefined){ | |
| res.render('index', {weather: null, error: 'Error, please try again'}); | |
| } else { | |
| let weatherText = `It's ${weather.main.temp} degrees in ${weather.name}!`; | |
| res.render('index', {weather: weatherText, error: null}); | |
| } | |
| } | |
| }); | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment