我正在尝试将变量从leaflet / jade传递给js / node文件。 我正在使用node + express + leaflet + js 我是按照以下方式进行的,但我没有得到这些值。 这是我正在使用的代码:
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 //呈现index.jade文件的索引文件
exports.index = function(req, res){
res.render('index');
};
index.jade //玉文件,我发送参数的行是" 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 //这里是我获取参数的地方,我通过控制台显示它们
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();
};
有什么想法吗?
答案 0 :(得分:1)
尝试更改导出模块的方式。在tiles.js
:
更改:
exports.tiles = function(req, res){ ...
致:
module.exports = function(req, res){
在app.js
:
app.get('/tiles', tiles.tiles)
到
app.get('/tiles', tiles)
<强>更新强>
此外,在设置标题和所需信息后添加res.end()
以将响应更改为已完成状态。
res.writeHead(200, {'content-type':'image/png'});
res.write(row.tile_data);
res.end();