let fetchData = {
urlGeo: 'http://api.openweathermap.org/geo/1.0/direct',
urlCurrent: 'http://api.openweathermap.org/data/2.5/weather',
urlTomorrow: 'http://api.openweathermap.org/data/2.5/forecast',
token: '',
lat: '',
lon: ''
}
async function requestWeather(a, b) {
const { data } = await axios.get(fetchData.urlCurrent+`?lat=${a}&lon=${b}&lang=ru&units=metric&appid=${fetchData.token}`)
return data;
}
async function requestGeo(search) {
const { data: { lat, lon }} = await axios.get(fetchData.urlGeo+`?q=${search}&appid=${fetchData.token}`)
return await requestWeather(lat, lon);
}
// SERVER ==========================================================================================
app.get('/city/:name', async (req, res) => {
const today = await requestGeo(req.params.name);
res.send(today);
});
app.listen(PORT, (req, res) => {
console.log(`Server work: http://localhost:${PORT}/`)
let today = {};
async function requestWeather(a, b) {
const res = await axios.get(fetchData.urlCurrent+`?lat=${a}&lon=${b}&lang=ru&units=metric&appid=${fetchData.token}`)
today = res.data;
}
async function requestGeo(search) {
const res = await axios.get(fetchData.urlGeo+`?q=${search}&appid=${fetchData.token}`)
fetchData.lat = await res.data[0].lat;
fetchData.lon = await res.data[0].lon;
requestWeather(fetchData.lat, fetchData.lon);
}