Skip to content

Instantly share code, notes, and snippets.

@bmorelli25
Created June 24, 2017 15:47
Show Gist options
  • Save bmorelli25/c67347560fe7b2213fea0d32d66fa53f to your computer and use it in GitHub Desktop.
Save bmorelli25/c67347560fe7b2213fea0d32d66fa53f to your computer and use it in GitHub Desktop.
app.post()
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