-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
50 lines (40 loc) · 1.48 KB
/
index.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
const express = require('express');
const axios = require('axios');
const app = express();
app.disable("x-powered-by");
app.use(express.static('public'));
app.get('/api/toilets', async (req, res) => {
const { lat, lon, radius, wheelchair, free } = req.query;
if (!lat || !lon || !radius) {
return res.status(400).json({ error: 'Missing required query parameters: lat, lon, radius' });
}
try {
// Construct the Overpass API query
let query = `
[out:json][timeout:5];
node["amenity"="toilets"](around:${radius},${lat},${lon})
`;
// Add wheelchair filter to the Overpass query
if (wheelchair === 'true') {
query += '["wheelchair"="yes"]';
}
query += ';out body;';
const response = await axios.get('https://overpass-api.de/api/interpreter', {
params: {
data: query,
},
});
// Filter results based on fee status
const filteredToilets = response.data.elements.filter((element) => {
const tags = element.tags || {};
return !(free === 'true' && tags.fee === 'yes'); // Exclude toilets that have a fee
});
res.json({ elements: filteredToilets });
} catch (error) {
res.status(500).json({ error: 'Failed to fetch toilet data' });
}
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});