-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
78 lines (62 loc) · 1.56 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const app = express();
app.use(cors());
app.use(bodyParser.json());
const GEOCODES = [
{
address: '29 Rue du 4 Septembre',
latitude: 48.86982,
longitude: 2.334579
},
{
address: '15 Rue de Bourgogne',
latitude: 48.8590453,
longitude: 2.3180404
}
];
const createError = (status, code, message) => {
const error = new Error(message);
error.code = code;
error.status = status;
return error;
};
const geocodeAddress = address => {
const geocode = GEOCODES.find(g => {
return address.toLowerCase().indexOf(g.address.toLowerCase()) >= 0;
});
if (!geocode) {
throw createError(400, 'GEOCODE_ERROR', `"${address}" cannot be geocoded.`);
}
return geocode;
};
app.post('/geocode', (req, res) => {
const geocode = geocodeAddress(req.body.address || '');
res.json(geocode);
});
app.post('/jobs', (req, res) => {
const { pickup, dropoff } = req.body;
if (!pickup || !dropoff) {
throw createError(400, 'JOB_ERROR', `"pickup" and "dropoff" are required`);
}
const job = {
pickup: geocodeAddress(pickup || ''),
dropoff: geocodeAddress(dropoff || '')
};
res.json(job);
});
app.use((err, req, res, next) => {
if (err.code && err.status) {
res.status(err.status).json({
code: err.code,
message: err.message
});
} else {
res.status(500).json({ code: 'FATAL' });
}
});
const port = process.env.PORT || 4000;
app.listen(port, () => {
console.log(`Listening on port ${port}`);
});