-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
27 lines (24 loc) · 979 Bytes
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
const express = require('express');
const axios = require('axios');
const app = express();
require('dotenv').config();
const PORT = 3000;
app.get('/query', async (req, res) => {
try {
const query = req.query.query;
const url = `https://nominatim.openstreetmap.org/?addressdetails=1&q=${query}&format=json&limit=1`;
const response = await axios.get(url);
const data = response.data;
const lat = data[0].lat;
const lon = data[0].lon;
const weatherUrl = `https://api.openweathermap.org/data/2.5/onecall?lat=${lat}&lon=${lon}&appid=${process.env.OPENWEATHER_API_KEY}&units=imperial`;
// // Make the API call to OpenWeatherMap using axios
const weatherResponse = await axios.get(weatherUrl);
const weatherData = weatherResponse.data;
res.send(weatherData);
} catch (error) {
console.error(error);
res.status(500).send('Internal server error');
}
});
app.listen(PORT, () => console.log(`Server listening on port ${PORT}`));