-
Notifications
You must be signed in to change notification settings - Fork 1
/
tile-server.js
50 lines (44 loc) · 1.35 KB
/
tile-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
48
49
50
var express = require("express"),
app = express(),
MBTiles = require('mbtiles');
if (process.argv.length < 3) {
console.log("Error! Missing TILES filename.\nUsage: node server.js TILES [PORT]");
process.exit(1);
}
var port = 3000;
if (process.argv.length === 4) {
port = parseInt(process.argv[3]);
}
var mbtilesLocation = String(process.argv[2]).replace(/\.mbtiles/,'') + '.mbtiles';
new MBTiles(mbtilesLocation, function(err, mbtiles) {
if (err) throw err;
app.get('/:z/:x/:y.*', function(req, res) {
var extension = req.param(0);
switch (extension) {
case "png": {
mbtiles.getTile(req.param('z'), req.param('x'), req.param('y'), function(err, tile, headers) {
if (err) {
res.status(404).send('Tile rendering error: ' + err + '\n');
} else {
res.header("Content-Type", "image/png")
res.send(tile);
}
});
break;
}
case "grid.json": {
mbtiles.getGrid(req.param('z'), req.param('x'), req.param('y'), function(err, grid, headers) {
if (err) {
res.status(404).send('Grid rendering error: ' + err + '\n');
} else {
res.header("Content-Type", "text/json")
res.send(grid);
}
});
break;
}
}
});
});
// actually create the server
app.listen(port);