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
-
27
1
var util = require ( 'util' ) ,
28
2
colors = require ( 'colors' ) ,
29
3
http = require ( 'http' ) ,
30
- httpProxy = require ( './../lib/node-http-proxy' ) ;
31
-
32
- //
33
- // url proxying middleware example.
4
+ httpProxy = require ( 'http-proxy' ) ;
5
+
34
6
//
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.
36
10
//
11
+
37
12
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.
40
20
return function ( url ) {
41
21
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 } ;
46
28
}
47
29
}
48
30
49
31
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 = [ ] ;
52
35
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 ] ) ) ;
54
38
}
55
39
40
+ // This closure is returned as the request handler.
56
41
return function ( req , res , next ) {
57
42
//
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)
61
46
//
62
47
var proxy = next ;
63
-
64
48
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.
67
54
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 ) ;
69
59
}
70
60
}
71
61
}
72
62
}
73
63
64
+ // Now we set up our proxy.
74
65
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
+ } )
76
73
) . listen ( 8000 ) ;
77
74
78
75
//
79
- // Target Http Server
76
+ // Target Http Server (to listen for requests on 'localhost')
80
77
//
81
78
http . createServer (
82
79
function ( req , res ) {
@@ -85,5 +82,6 @@ http.createServer(
85
82
res . end ( ) ;
86
83
} ) . listen ( 9000 ) ;
87
84
85
+ // And finally, some colored startup output.
88
86
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