-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.js
executable file
·67 lines (62 loc) · 1.8 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env node
var fs = require('fs'),
parse = require('url').parse,
http = require('http'),
createServer = http.createServer,
STATUS_CODES = http.STATUS_CODES,
printlet = require('../lib/printlet'),
args = process.argv.slice(2),
port = parseInt(args[0] || 41462),
tileJson = require(
(args[1] != null) ? process.cwd()+'/'+args[1] : './osm.json'
),
render = printlet(tileJson),
server;
server = createServer(function(req, res) {
try {
var url = parse(req.url, true),
pathname = url.pathname,
query = url.query,
params = pathname.substr(1).split('/'),
zoom = parseInt(params[0]),
lng = parseFloat(params[1]),
lat = parseFloat(params[2]),
dimensions = params[3].split('x'),
end = dimensions[1].split('.'),
width = parseInt(dimensions[0]),
height = parseInt(end[0]),
format = end[1],
opt = {
width: width,
height: height,
zoom: zoom,
lat: lat,
lng: lng,
format: format
};
if ((tileJson.maxzoom != null && zoom > tileJson.maxzoom) ||
(tileJson.minzoom != null && zoom < tileJson.minzoom)) {
res.writeHead(400);
res.end(STATUS_CODES[400] + ": Supplied zoom level is out of bounds.");
return;
}
if (query.geojson != null) {
opt.geojson = JSON.parse(query.geojson);
}
render(opt, function(err, data) {
if (err != null) {
res.writeHead(500);
res.end(STATUS_CODES[500] + ": " + err);
} else {
res.writeHead(200, {
'Content-Type': 'image/'+format
});
data.stream.pipe(res);
}
});
} catch (err) {
res.writeHead(500);
res.end(STATUS_CODES[500] + ": " + err);
}
});
server.listen(port);