I am trying to pass variables from leaflet/jade to a js/node file. I am using node + express + leaflet + js I am doing it the following way but I am not getting the values. Here is the code I am using:
app.js
var express = require('express');
var routes = require('./routes');
var tiles = require('./routes/tiles');
var http = require('http');
var path = require('path');
var app = express();
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/tiles', tiles.tiles);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
index.js //the index file that renders the index.jade file
exports.index = function(req, res){
res.render('index');
};
index.jade //the jade file, the line where I am sending the params is "tiles?z={z}&x={x}&y={y}"
doctype html
html
head
title Tile Server Example
meta(charset='utf-8')
meta(name='viewport', content='width=device-width, initial-scale=1.0')
link(rel='stylesheet', href='http://cdn.leafletjs.com/leaflet-0.7.1/leaflet.css')
body
#map(style='width: 800px; height: 800px')
a(href='tiles') < tiles >
script(src='http://cdn.leafletjs.com/leaflet-0.7.1/leaflet.js')
script.
var map;
var mbTiles = new L.tileLayer('tiles?z={z}&x={x}&y={y}', {
tms: true,
attribution: 'this is a test',
opacity: 0.7
});
map = new L.Map("map",{
zoom: 18,
center: [12.83563,77.68312],
layers: [mbTiles]
});
tiles.js //here is where I get the params, I am displaying them via console
var x = "", y = "", z = "";
module.exports = function(req, res){
x = req.query.x;
y = req.query.y;
z = req.query.z;
console.log("z="+z+"&x="+x+"&y="+y);
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('routes/tiles.mbtiles');
var sql = "SELECT * FROM tiles WHERE zoom_level = "+z+" AND tile_column = "+x+" AND tile_row = "+y;
db.each(sql, function(err, row) {
console.log(sql);
res.writeHead(200, {'content-type':'image/jpg'});
res.write(row.tile_data);
res.end();
});
db.close();
};
Any idea?
Try to change, the way you export your modules. In tiles.js
:
Change:
exports.tiles = function(req, res){ ...
To:
module.exports = function(req, res){
And in app.js
:
app.get('/tiles', tiles.tiles)
To
app.get('/tiles', tiles)
UPDATE
Also, add res.end()
after setting headers and needed information to change response into a finished state.
res.writeHead(200, {'content-type':'image/png'});
res.write(row.tile_data);
res.end();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.