Skip to content

Commit 4cc18f4

Browse files
AvianFludominictarr
authored andcommitted
Tested & fixed url middleware example, added comments.
1 parent 8b48b7e commit 4cc18f4

File tree

1 file changed

+48
-50
lines changed

1 file changed

+48
-50
lines changed

examples/url-middleware.js

+48-50
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,79 @@
1-
/*
2-
urls-middleware.js: Basic example of middleware in node-http-proxy
3-
4-
Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, Fedor Indutny, Marak Squires, & Dominic Tarr.
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-
271
var util = require('util'),
282
colors = require('colors'),
293
http = require('http'),
30-
httpProxy = require('./../lib/node-http-proxy');
31-
32-
//
33-
// url proxying middleware example.
4+
httpProxy = require('http-proxy');
5+
346
//
35-
// this is not optimised or tested but shows the basic approch to writing a middleware.
7+
// This is an example of a url-routing middleware.
8+
// This is not intended for production use, but rather as
9+
// an example of how to write a middleware.
3610
//
11+
3712
function matcher (url, dest) {
38-
var r = new RegExp (url)
39-
13+
// First, turn the URL into a regex.
14+
// NOTE: Turning user input directly into a Regular Expression is NOT SAFE.
15+
var r = new RegExp(url.replace(/\//, '\\/'));
16+
// This next block of code may look a little confusing.
17+
// It returns a closure (anonymous function) for each URL to be matched,
18+
// storing them in an array - on each request, if the URL matches one that has
19+
// a function stored for it, the function will be called.
4020
return function (url) {
4121
var m = r(url)
42-
if (!m) return
43-
var path = url.slice(m[0].length)
44-
console.log('proxy:', url, '->', path)
45-
return {url: path, dest: dest}
22+
if (!m) {
23+
return;
24+
}
25+
var path = url.slice(m[0].length);
26+
console.log('proxy:', url, '->', dest);
27+
return {url: path, dest: dest};
4628
}
4729
}
4830

4931
exports.urls = function (urls) {
50-
51-
var matchers = []
32+
// This is the entry point for our middleware.
33+
// 'matchers' is the array of URL matchers, as mentioned above.
34+
var matchers = [];
5235
for (var url in urls) {
53-
matchers.push(matcher(url, urls[url]))
36+
// Call the 'matcher' function above, and store the resulting closure.
37+
matchers.push(matcher(url, urls[url]));
5438
}
5539

40+
// This closure is returned as the request handler.
5641
return function (req, res, next) {
5742
//
58-
// in nhp middlewares, `proxy` is the prototype of `next`
59-
// (this means nhp middlewares support both connect API (req, res, next)
60-
// and nhp API (req, res, proxy)
43+
// in node-http-proxy middlewares, `proxy` is the prototype of `next`
44+
// (this means node-http-proxy middlewares support both the connect API (req, res, next)
45+
// and the node-http-proxy API (req, res, proxy)
6146
//
6247
var proxy = next;
63-
6448
for (var k in matchers) {
65-
var m;
66-
if (m = matchers[k](req.url)) {
49+
// for each URL matcher, try the request's URL.
50+
var m = matchers[k](req.url);
51+
// If it's a match:
52+
if (m) {
53+
// Replace the local URL with the destination URL.
6754
req.url = m.url;
68-
return proxy.proxyRequest(req,res, m.dest);
55+
// If routing to a server on another domain, the hostname in the request must be changed.
56+
req.headers.host = m.host;
57+
// Once any changes are taken care of, this line makes the magic happen.
58+
proxy.proxyRequest(req, res, m.dest);
6959
}
7060
}
7161
}
7262
}
7363

64+
// Now we set up our proxy.
7465
httpProxy.createServer(
75-
exports.urls({'/hello': {port: 9000, host: 'localhost'}})
66+
// This is where our middlewares go, with any options desired - in this case,
67+
// the list of routes/URLs and their destinations.
68+
exports.urls({
69+
'/hello': { port: 9000, host: 'localhost' },
70+
'/charlie': { port: 80, host: 'charlieistheman.com' },
71+
'/google': { port: 80, host: 'google.com' }
72+
})
7673
).listen(8000);
7774

7875
//
79-
// Target Http Server
76+
// Target Http Server (to listen for requests on 'localhost')
8077
//
8178
http.createServer(
8279
function (req, res) {
@@ -85,5 +82,6 @@ http.createServer(
8582
res.end();
8683
}).listen(9000);
8784

85+
// And finally, some colored startup output.
8886
util.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8000'.yellow);
89-
util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9000 '.yellow);
87+
util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9000 '.yellow);

0 commit comments

Comments
 (0)