-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
96 lines (78 loc) · 2.51 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// var flo = require('fb-flo'),
// path = require('path');
//
// var server = flo(
// ".",
// {
// port: 8001,
// host: 'localhost',
// verbose: false,
// glob: ['*.js', '*.css']
// },
// null
// );
var express = require('express');
var app = express();
var http = require('http');
app.configure (function() {
app.use (express.methodOverride());
app.use (express.cookieParser ());
app.use (express.bodyParser());
app.use (function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
});
app.use ("/openstreetmap", function(req, res, next) {
var proxy = http.createClient (80, "tile.openstreetmap.org")
var proxy_request = proxy.request (req.method, req.url, req.headers);
proxy_request.addListener ('response', function (proxy_response) {
proxy_response.addListener ('data', function(chunk) {
res.write (chunk, 'binary');
});
proxy_response.addListener('end', function() {
res.end ();
});
res.writeHead(proxy_response.statusCode, proxy_response.headers);
});
req.addListener('data', function(chunk) {
proxy_request.write (chunk, 'binary');
});
req.addListener('end', function() {
proxy_request.end ();
});
});
app.use (express.static(__dirname));
app.use (app.router);
});
var http = require('http');
app.get('/', function(req, res){
res.send('hello world');
});
app.get ("/openstreetmap", function (req, res, next) {
console.log ("proxy openstreetmap");
next();
return;
var url = req.url.replace ("/openstreetmap/", "http://tile.openstreetmap.org/");
var proxy = http.createClient (80, "http://tile.openstreetmap.org/")
var proxy_request = proxy.req (req.method, url, req.headers);
proxy_request.addListener ('response', function (proxy_response) {
proxy_response.addListener ('data', function(chunk) {
res.write (chunk, 'binary');
});
proxy_response.addListener('end', function() {
res.end ();
});
res.writeHead(proxy_response.statusCode, proxy_response.headers);
});
req.addListener('data', function(chunk) {
proxy_request.write (chunk, 'binary');
});
req.addListener('end', function() {
proxy_request.end ();
});
});
app.listen (8000);
console.log('express running at http://localhost:%d', 8000);