-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (39 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
require("dotenv").config();
const express = require("express");
const providers = require('./geocoding-providers');
const findArea = require('./polygons');
const app = express();
const validate = (req, res, next) => {
// todo Use joi here to validate the query string parameter -> address
return next()
};
// Apply a recursive call to each existing provider into the array.
// Responds with success ('OK' status) only when a valuable result has been returned.
// In other case, proceeds to the next provider, if exists or responds with 'NOT_FOUND' status
const retryAcrossProviders = async (providers, address) => {
const [currentProvider, ...rest] = providers;
let res = await currentProvider.request(address).catch(e => e);
if (res.status === 'OK') return res;
return (rest.length > 0)
? retryAcrossProviders(rest, address)
: res;
};
// Function to handle the info path
app.get('/info', validate, async (req, res) => {
// Access the provided 'address' query parameter
let search = req.query.address;
try {
let response = await retryAcrossProviders(providers, encodeURI(search));
if (response.status === 'OK'){
let {name} = findArea([response.location.long, response.location.lat]);
response.location.serviceArea = name;
}
return res.send(response);
} catch (e) {
console.log(e)
res.send(500)
}
});
app.listen(5000, () => {
console.log('Server is listening on port 5000')
});