Skip to content

Instantly share code, notes, and snippets.

@manuelroth
Last active May 18, 2023 07:51

Revisions

  1. Manuel Roth revised this gist May 11, 2016. No changes.
  2. Manuel Roth revised this gist May 11, 2016. No changes.
  3. Manuel Roth revised this gist May 11, 2016. No changes.
  4. Manuel Roth revised this gist May 11, 2016. 1 changed file with 1 addition and 6 deletions.
    7 changes: 1 addition & 6 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -19,12 +19,7 @@ app.get('/:source/:z/:x/:y.pbf', function(req, res) {
    res.set({"Content-Type": "text/plain"});
    res.status(404).send('Tile rendering error: ' + err + '\n');
    } else {
    res.set({
    "Access-Control-Allow-Origin":"*",
    "Access-Control-Allow-Headers":"Origin, X-Requested-With, Content-Type, Accept",
    "Content-Type":"application/x-protobuf",
    "Content-Encoding":"gzip"
    });
    res.set(header);
    res.send(tile);
    }
    });
  5. Manuel Roth revised this gist May 11, 2016. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion index.js
    Original file line number Diff line number Diff line change
    @@ -11,6 +11,7 @@ var header = {
    "Content-Encoding":"gzip"
    };

    // Route which handles requests like the following: /<mbtiles-name>/0/1/2.pbf
    app.get('/:source/:z/:x/:y.pbf', function(req, res) {
    new MBTiles(p.join(__dirname, req.params.source + '.mbtiles'), function(err, mbtiles) {
    mbtiles.getTile(req.params.z, req.params.x, req.params.y, function(err, tile, headers) {
    @@ -31,6 +32,6 @@ app.get('/:source/:z/:x/:y.pbf', function(req, res) {
    });
    });

    // start up the server
    // Starts up the server on port 3000
    console.log('Listening on port: ' + 3000);
    app.listen(3000);
  6. Manuel Roth created this gist May 11, 2016.
    36 changes: 36 additions & 0 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    var express = require("express"),
    app = express(),
    MBTiles = require('mbtiles'),
    p = require("path");

    // Enable CORS and set correct mime type/content encoding
    var header = {
    "Access-Control-Allow-Origin":"*",
    "Access-Control-Allow-Headers":"Origin, X-Requested-With, Content-Type, Accept",
    "Content-Type":"application/x-protobuf",
    "Content-Encoding":"gzip"
    };

    app.get('/:source/:z/:x/:y.pbf', function(req, res) {
    new MBTiles(p.join(__dirname, req.params.source + '.mbtiles'), function(err, mbtiles) {
    mbtiles.getTile(req.params.z, req.params.x, req.params.y, function(err, tile, headers) {
    if (err) {
    res.set({"Content-Type": "text/plain"});
    res.status(404).send('Tile rendering error: ' + err + '\n');
    } else {
    res.set({
    "Access-Control-Allow-Origin":"*",
    "Access-Control-Allow-Headers":"Origin, X-Requested-With, Content-Type, Accept",
    "Content-Type":"application/x-protobuf",
    "Content-Encoding":"gzip"
    });
    res.send(tile);
    }
    });
    if (err) console.log("error opening database");
    });
    });

    // start up the server
    console.log('Listening on port: ' + 3000);
    app.listen(3000);