forked from tobinbradley/mbtiles-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
87 lines (77 loc) · 2.7 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
79
80
81
82
83
84
85
86
87
var express = require("express"),
app = express(),
MBTiles = require('@mapbox/mbtiles'),
p = require("path"),
config = require('config');
// path to the mbtiles; default is the server.js directory
var tilesDir = p.join(__dirname, 'data');
var port = config.port || 8084;
var protocol = config.protocol;
// Set return header
function getContentType(t) {
var header = {};
// CORS
// header["Access-Control-Allow-Origin"] = "*";
// header["Access-Control-Allow-Headers"] = "Origin, X-Requested-With, Content-Type, Accept";
// Cache
header["Cache-Control"] = "no-cache, no-store, must-revalidate";
// request specific headers
if (t === "png") {
header["Content-Type"] = "image/png";
}
if (t === "jpg") {
header["Content-Type"] = "image/jpeg";
}
if (t === "pbf") {
header["Content-Type"] = "application/x-protobuf";
header["Content-Encoding"] = "gzip";
}
if (t === "json") {
header["Content-Type"] = "application/json";
}
return header;
}
app.get('/:s/:d.json', function(req, res) {
if(!protocol) {
protocol = req.protocol;
}
new MBTiles(p.join(tilesDir, req.params.s + '.mbtiles'), function(err, mbtiles) {
mbtiles.getInfo(function(err, info){
if(err) {
console.log("getInfo called on", p.join(tilesDir, req.params.s + '.mbtiles'), "resulted in error:", err);
} else {
info.tiles = [protocol + "://" + req.hostname + "/vector/" + req.params.s + "/{z}/{x}/{y}.pbf"]
// info.tiles = [protocol + "://" + req.hostname + `:${port}/` + req.params.s + "/{z}/{x}/{y}.pbf"]
res.set(getContentType(req.params.t));
res.send(info);
}
});
});
});
// tile cannon
app.get('/:s/:z/:x/:y.:t', function(req, res) {
new MBTiles(p.join(tilesDir, req.params.s + '.mbtiles'), function(err, mbtiles) {
mbtiles.getTile(req.params.z, req.params.x, req.params.y, function(err, tile, headers) {
if (err) {
if( err == 'Error: Tile does not exist') {
res.set(getContentType(req.params.t));
res.send('');
} else {
var header = {};
// header["Access-Control-Allow-Origin"] = "*";
// header["Access-Control-Allow-Headers"] = "Origin, X-Requested-With, Content-Type, Accept";
header["Content-Type"] = "text/plain";
res.set(header);
res.status(404).send('Tile rendering error: ' + err + '\n');
}
} else {
res.set(getContentType(req.params.t));
res.send(tile);
}
});
if (err) console.log("error opening database");
});
});
// start up the server
console.log('Listening on port: ' + port);
app.listen(port);