-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update script-weatherNode.js to fetch and display weather for default…
… city on page load and for user-input city on button click.
- Loading branch information
Showing
1 changed file
with
18 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,25 @@ | ||
document.addEventListener('DOMContentLoaded', function() { | ||
fetch('/weather') | ||
.then(response => response.json()) | ||
function fetchWeather() { | ||
fetch('/weather?city=${city}') | ||
.then(reponse => response.json()) | ||
.then(data => { | ||
const weatherDiv = document.getElementById('Weather'); | ||
const content = `Temperatura: ${data.main.temp}°C<br> | ||
const content = `Cidade: ${city}<br> | ||
Temperatura: ${data.main.temp}°C<br> | ||
Clima: ${data.weather[0].main}<br> | ||
Descrição: ${data.weather[0].description}`; | ||
weatherDiv.innerHTML = content; | ||
}) | ||
.catch(error => console.error('Erro ao buscar dados: ', error)); | ||
}); | ||
} | ||
|
||
// Evento que carrega a previsão do tempo para uma cidade padrão ao carregar a página. | ||
document.addEventListener('DOMContentLoaded', function() { | ||
const defaulttCity('Limeira'); | ||
fetchWeather('deafultCity') | ||
}); | ||
|
||
// Evento de clique do botão que busca a previsão do tempo para a cidade inserida pelo usuário. | ||
document.getElementById('checkWeather').addEventListener('click', function() { | ||
const city = document.getElementById('cityInput').value; | ||
fetchWeather(city); | ||
}); |