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
Cannot GET /
error as you.