-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
57 lines (44 loc) · 1.58 KB
/
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const express = require('express');
const cors = require('cors');
const axios = require('axios');
const app = express();
const PORT = 5000;
const OPENCAGE_API_KEY = '1fc169c5e1a64d5093cce71f46c09794';
app.use(cors());
app.use(express.json());
const getCoordinatesFromPincode = async (pincode) => {
try {
const response = await axios.get(`https://api.opencagedata.com/geocode/v1/json`, {
params: {
q: pincode,
key: OPENCAGE_API_KEY,
},
});
if (response.data.results.length > 0) {
const { lat, lng } = response.data.results[0].geometry;
return { latitude: lat, longitude: lng };
} else {
return { latitude: null, longitude: null };
}
} catch (error) {
console.error('Error fetching coordinates:', error);
return { latitude: null, longitude: null };
}
};
app.get('/api/nearest-plants', async (req, res) => {
const { pincode } = req.query;
try {
const { latitude, longitude } = await getCoordinatesFromPincode(pincode);
if (!latitude || !longitude) {
return res.status(400).json({ error: 'Invalid pincode or no coordinates found' });
}
const apiUrl = `https://api.example.com/plants?lat=${latitude}&lng=${longitude}`;
const response = await axios.get(apiUrl);
res.json(response.data);
} catch (error) {
res.status(500).json({ error: 'Error fetching data' });
}
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});