-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
47 lines (33 loc) · 1.02 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
const express = require('express')
const morgan = require('morgan')
const cors = require('cors')
const createError = require('http-errors')
const Flatbush = require('flatbush')
const feuilles = require('./feuilles.json')
/* Index */
const index = new Flatbush(feuilles.length)
for (const {bbox} of feuilles) {
index.add(bbox[0], bbox[1], bbox[2], bbox[3])
}
index.finish()
/* API */
const app = express()
if (process.env.NODE_ENV !== 'production') {
app.use(morgan('dev'))
}
app.use(cors({origin: true}))
app.get('/feuilles', (req, res) => {
if (!req.query.bbox) {
throw createError(400, 'bbox is required')
}
const bbox = req.query.bbox.split(',').map(n => Number.parseFloat(n))
if (bbox.length !== 4 || bbox.some(n => Number.isNaN(n))) {
throw createError(400, 'bbox is malformed')
}
const results = index.search(...bbox).map(i => feuilles[i])
res.send(results.map(({id}) => ({id})))
})
const port = process.env.PORT || 5000
app.listen(port, () => {
console.log('Start listening on port ' + port)
})