-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
88 lines (67 loc) · 1.97 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
var http = require('http'),
var ejs = require("ejs");
var fs = require("fs");
var express = require('express');
var app = express();
app.use("/", function(req, res,next) {
var URL = req.url.split("/");
if(URL.length >= 4){
getMap( URL[1], URL[2] , URL[3] , URL[4],function(err,data){
res.writeHead(200, {
'Content-Type': 'image/png'
});
res.write(data);
//data.pipe(res);
res.end();
});
}else{
next();
}
});
app.use("/",function(req, res) {
var indexFile = fs.readFileSync(__dirname + "/index.html");
res.writeHead(200, {
'Content-Type': 'text/html'
});
res.write(ejs.render(indexFile.toString()));
res.end();
});
httpProxy.createServer(function(req, res, proxy) {
req.mainProxy = proxy;
app(req, res);
}).listen(process.env.PORT, process.env.IP);
function getMap(s,z,x,y,callback){
var savePath = __dirname + "/map_tiles/" + z + "-" + x + "-" + y;
fs.exists(savePath, function (exists) {
if(exists)
fileFetch();
else
fetch();
});
function fileFetch(){
fs.readFile(savePath, function(err,imagedata){
if (err) throw err;
callback(err,imagedata);
});
}
function fetch(){
var options = {
host: s+'.tile.osm.org'
, port: 80
, path: "/" + z + "/" + x + "/" + y
};
http.get(options, function(res){
var imagedata = '';
res.setEncoding('binary');
res.on('data', function(chunk){
imagedata += chunk;
});
res.on('end', function(){
fs.writeFile(savePath, imagedata, 'binary', function(err){
if (err) throw err;
fileFetch();
});
});
});
}
}