-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
52 lines (45 loc) · 1.28 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
const config = require('config')
const express = require('express')
const cors = require('cors')
const MBTiles = require('@mapbox/mbtiles')
const htdocsDir = config.get('htdocsDir')
const fontsDir = config.get('fontsDir')
const mbtilesPath = config.get('mbtilesPath')
const port = config.get('port')
const app = express()
app.use(cors())
app.use('/', express.static(htdocsDir))
app.use('/fonts', express.static(fontsDir))
const openMbtiles = () => {
return new Promise((resolve, reject) => {
new MBTiles(`${mbtilesPath}?mode=ro`, (err, mbtiles) => {
if (err) { reject(err) }
resolve(mbtiles)
})
})
}
let mbtiles
openMbtiles().then(m => {
mbtiles = m
})
const getTile = (z, x, y) => {
return new Promise((resolve, reject) => {
mbtiles.getTile(z, x, y, (err, tile, headers) => {
if (err) reject(err)
resolve(tile)
})
})
}
app.get('/zxy/:z/:x/:y.pbf', async (req, res) => {
const z = parseInt(req.params.z)
const x = parseInt(req.params.x)
const y = parseInt(req.params.y)
getTile(z, x, y).then(tile => {
res.set('content-type', 'application/vnd.mapbox-vector-tile')
res.set('content-encoding', 'gzip')
res.send(tile)
}).catch(e => {
res.status(404).send(`tilenot found /zxy/${z}/${x}/${y}.pbf: ${e}`)
})
})
app.listen(port)