0

I'm trying to load a .mbtiles file into my browser using the tilelive and mbtiles module. The "lon.mbtiles" is a sample city (London) downloaded directly from mapbox's website, and should be loadable, unless mapbox made an error on their end.

Here's my server.js:

var express = require('express');
var http = require('http');
var app = express();
var tilelive = require('tilelive');
require('mbtiles').registerProtocols(tilelive);

tilelive.load('mbtiles://lon.mbtiles', function(err, source) {
    console.log("Server has connected");
if (err) {
    throw err;
    console.log("mbtiles file not found, please ensure the path is correct in server.js");
}

app.set('port', 7777);

// USED FOR CROSS-SERVER COMMS, NOT NECESSARY RIGHT NOW

// app.use(function(req, res, next) {
//     res.header("Access-Control-Allow-Origin", "*");
//     res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
//     next();
// });


// ** Test function for localhost connection, works fine with port 7777 as is stands **

// app.get("/", function(req, res) {
//         res.send("test");
// });



app.get(/^\/v2\/tiles\/(\d+)\/(\d+)\/(\d+).pbf$/, function(req, res){

    var z = req.params[0];
    var x = req.params[1];
    var y = req.params[2];

    console.log('get tile %d, %d, %d', z, x, y);

    source.getTile(z, x, y, function(err, tile, headers) {
        if (err) {
            res.status(404)
            res.send(err.message);
            //console.log(err.message);
        } else {
          res.set(headers);
          res.send(tile);
        }
    });
});

http.createServer(app).listen(app.get('port'), function() {
    console.log('Express server listening on port ' + app.get('port'));
     });
});

the script runs and detects the tile if ran from the local file, but I can't get the localhost to work. I've tested (in the code) to see if I was having firewall problems blocking my localhost but it worked just fine.

From my understanding it's a problem with the app.get line, though my knowledge of NodeJS doesn't extend past 2 days of videos and articles.

How can I get my localhost to actually connect and not throw back errors?

Thanks in advance

James Gould
  • 4,492
  • 2
  • 27
  • 50
  • "How can I get my localhost to actually connect and not throw back errors?" - please be specific: what kind of errors, exactly? What are the error messages, exactly? – tmcw Jul 05 '16 at 14:25
  • @Jay Gould were you able to fix this? I'm getting the express server up and running but when i try to connect, my localhost gives the same `Cannot GET /` error as you. – lve May 14 '20 at 14:33

1 Answers1

0

I had to use three slashes to load the .mbtile try 'mbtiles:///lon.mbtiles'

I also had to install sqlite require('sqlite3').verbose();

Owen J Lamb
  • 254
  • 1
  • 10