Skip to content

Commit f0917a3

Browse files
indexzerodominictarr
authored andcommitted
[minor] Style updates and whitespace cleaning for consistency
1 parent 8eaec35 commit f0917a3

5 files changed

+241
-159
lines changed

examples/body-decoder.js

+32-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,34 @@
1-
#!/usr/local/bin/node
1+
/*
2+
body-decoder.js: Example of body-decoder middleware with node-http-proxy
3+
4+
Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, Fedor Indutny, & Marak Squires.
5+
6+
Permission is hereby granted, free of charge, to any person obtaining
7+
a copy of this software and associated documentation files (the
8+
"Software"), to deal in the Software without restriction, including
9+
without limitation the rights to use, copy, modify, merge, publish,
10+
distribute, sublicense, and/or sell copies of the Software, and to
11+
permit persons to whom the Software is furnished to do so, subject to
12+
the following conditions:
13+
14+
The above copyright notice and this permission notice shall be
15+
included in all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
25+
*/
226

327
var httpProxy = require('http-proxy'),
428
http = require('http'),
529
util = require('util'),
630
colors = require('colors');
731

8-
932
exports.bodyMod = function () {
1033
console.log('middleware has been started.'.green);
1134
return function (req, res, next) {
@@ -16,16 +39,22 @@ exports.bodyMod = function () {
1639
console.log('ON DATA')
1740
total += data;
1841
});
42+
1943
req.on('end', function () {
2044
console.log('ON END')
2145
console.log(total);
46+
//
2247
// This line, uncommented, hangs forever.
2348
// proxy.proxyRequest(req, res, { port: 9000, host: 'localhost' });
2449
// The following also hangs forever.
2550
// next.proxyRequest(req, res, { port: 9000, host: 'localhost' });
26-
})
51+
//
52+
});
53+
54+
//
2755
// The following fires just fine.
2856
//proxy.proxyRequest(req, res, { port: 9000, host: 'localhost' });
57+
//
2958
console.log('request proxied...'.blue);
3059
}
3160
}

examples/gzip-middleware.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
gzip-middleware.js: Basic example of middleware in node-http-proxy
2+
gzip-middleware.js: Basic example of `connect-gzip` middleware in node-http-proxy
33
44
Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, Fedor Indutny, Marak Squires, & Dominic Tarr.
55
@@ -32,7 +32,6 @@ var util = require('util'),
3232
//
3333
// Basic Http Proxy Server
3434
//
35-
3635
httpProxy.createServer(
3736
require('connect-gzip').gzip({ matchType: /.?/ }),
3837
9000, 'localhost'
@@ -41,12 +40,11 @@ httpProxy.createServer(
4140
//
4241
// Target Http Server
4342
//
44-
http.createServer(
45-
function (req, res) {
46-
res.writeHead(200, { 'Content-Type': 'text/plain' });
47-
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
48-
res.end();
49-
}).listen(9000);
43+
http.createServer(function (req, res) {
44+
res.writeHead(200, { 'Content-Type': 'text/plain' });
45+
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
46+
res.end();
47+
}).listen(9000);
5048

5149
util.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8000'.yellow);
5250
util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9000 '.yellow);

examples/url-middleware.js

+45-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,29 @@
1+
/*
2+
url-middleware.js: Example of a simple url routing middleware for node-http-proxy
3+
4+
Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, Fedor Indutny, & Marak Squires.
5+
6+
Permission is hereby granted, free of charge, to any person obtaining
7+
a copy of this software and associated documentation files (the
8+
"Software"), to deal in the Software without restriction, including
9+
without limitation the rights to use, copy, modify, merge, publish,
10+
distribute, sublicense, and/or sell copies of the Software, and to
11+
permit persons to whom the Software is furnished to do so, subject to
12+
the following conditions:
13+
14+
The above copyright notice and this permission notice shall be
15+
included in all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
25+
*/
26+
127
var util = require('util'),
228
colors = require('colors'),
329
http = require('http'),
@@ -10,21 +36,29 @@ var util = require('util'),
1036
//
1137

1238
function matcher (url, dest) {
39+
//
1340
// First, turn the URL into a regex.
1441
// NOTE: Turning user input directly into a Regular Expression is NOT SAFE.
42+
//
1543
var r = new RegExp(url.replace(/\//, '\\/'));
44+
45+
//
1646
// This next block of code may look a little confusing.
1747
// It returns a closure (anonymous function) for each URL to be matched,
1848
// storing them in an array - on each request, if the URL matches one that has
1949
// a function stored for it, the function will be called.
50+
//
2051
return function (url) {
2152
var m = r(url)
2253
if (!m) {
2354
return;
2455
}
2556
var path = url.slice(m[0].length);
2657
console.log('proxy:', url, '->', dest);
27-
return {url: path, dest: dest};
58+
return {
59+
url: path,
60+
dest: dest
61+
};
2862
}
2963
}
3064

@@ -61,26 +95,29 @@ exports.urls = function (urls) {
6195
}
6296
}
6397

98+
//
6499
// Now we set up our proxy.
100+
//
65101
httpProxy.createServer(
102+
//
66103
// This is where our middlewares go, with any options desired - in this case,
67104
// the list of routes/URLs and their destinations.
105+
//
68106
exports.urls({
69107
'/hello': { port: 9000, host: 'localhost' },
70108
'/charlie': { port: 80, host: 'charlieistheman.com' },
71109
'/google': { port: 80, host: 'google.com' }
72-
})
110+
});
73111
).listen(8000);
74112

75113
//
76114
// Target Http Server (to listen for requests on 'localhost')
77115
//
78-
http.createServer(
79-
function (req, res) {
80-
res.writeHead(200, { 'Content-Type': 'text/plain' });
81-
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
82-
res.end();
83-
}).listen(9000);
116+
http.createServer(function (req, res) {
117+
res.writeHead(200, { 'Content-Type': 'text/plain' });
118+
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
119+
res.end();
120+
}).listen(9000);
84121

85122
// And finally, some colored startup output.
86123
util.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8000'.yellow);

examples/web-socket-proxy.js

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ var server = http.createServer(function (req, res) {
4747
res.writeHead(200);
4848
res.end();
4949
});
50+
5051
server.listen(8080);
5152

5253
//

0 commit comments

Comments
 (0)