-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
69 lines (58 loc) · 1.82 KB
/
index.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
68
69
'use strict';
const express = require('express');
const mbgl = require('mapbox-gl-native');
const request = require('request');
const sharp = require('sharp');
const config = require('./config');
const app = express();
let map = new mbgl.Map({
request: function (req, callback) {
request({
url: req.url,
encoding: null,
gzip: true
}, function (err, res, body) {
if (err) {
callback(err);
} else if (res.statusCode == 200) {
var response = {};
if (res.headers.modified) { response.modified = new Date(res.headers.modified); }
if (res.headers.expires) { response.expires = new Date(res.headers.expires); }
if (res.headers.etag) { response.etag = res.headers.etag; }
response.data = body;
callback(null, response);
} else {
callback(new Error(JSON.parse(body).message));
}
});
}
});
map.load(config.STYLE);
app.get('/:z/:x/:y', function (req, res) {
let options = {
zoom: req.params.z,
center: [req.params.x, req.params.y]
};
map.render(options, function (err, buffer) {
if (err) {
console.error(err);
res.send(err);
} else {
let image = sharp(buffer, {
raw: {
width: 512,
height: 512,
channels: 4
}
});
res.set('Content-Type', 'image/png');
image.png().toBuffer()
.then((result) => {
res.send(result);
});
}
});
});
app.listen(config.SERVER_PORT, function () {
console.log(`Server app listening on port ${config.SERVER_PORT}!`);
});