forked from tobinbradley/mbtiles-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (49 loc) · 1.64 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
var express = require("express"),
app = express(),
MBTiles = require('mbtiles'),
p = require("path");
// path to the mbtiles; default is the server.js directory
var tilesDir = __dirname;
// 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"] = "public, max-age=604800";
// 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";
}
return header;
}
// 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) {
let 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: ' + 3000);
app.listen(3000);