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
+
1
27
var util = require ( 'util' ) ,
2
28
colors = require ( 'colors' ) ,
3
29
http = require ( 'http' ) ,
@@ -10,21 +36,29 @@ var util = require('util'),
10
36
//
11
37
12
38
function matcher ( url , dest ) {
39
+ //
13
40
// First, turn the URL into a regex.
14
41
// NOTE: Turning user input directly into a Regular Expression is NOT SAFE.
42
+ //
15
43
var r = new RegExp ( url . replace ( / \/ / , '\\/' ) ) ;
44
+
45
+ //
16
46
// This next block of code may look a little confusing.
17
47
// It returns a closure (anonymous function) for each URL to be matched,
18
48
// storing them in an array - on each request, if the URL matches one that has
19
49
// a function stored for it, the function will be called.
50
+ //
20
51
return function ( url ) {
21
52
var m = r ( url )
22
53
if ( ! m ) {
23
54
return ;
24
55
}
25
56
var path = url . slice ( m [ 0 ] . length ) ;
26
57
console . log ( 'proxy:' , url , '->' , dest ) ;
27
- return { url : path , dest : dest } ;
58
+ return {
59
+ url : path ,
60
+ dest : dest
61
+ } ;
28
62
}
29
63
}
30
64
@@ -61,26 +95,29 @@ exports.urls = function (urls) {
61
95
}
62
96
}
63
97
98
+ //
64
99
// Now we set up our proxy.
100
+ //
65
101
httpProxy . createServer (
102
+ //
66
103
// This is where our middlewares go, with any options desired - in this case,
67
104
// the list of routes/URLs and their destinations.
105
+ //
68
106
exports . urls ( {
69
107
'/hello' : { port : 9000 , host : 'localhost' } ,
70
108
'/charlie' : { port : 80 , host : 'charlieistheman.com' } ,
71
109
'/google' : { port : 80 , host : 'google.com' }
72
- } )
110
+ } ) ;
73
111
) . listen ( 8000 ) ;
74
112
75
113
//
76
114
// Target Http Server (to listen for requests on 'localhost')
77
115
//
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 ) ;
84
121
85
122
// And finally, some colored startup output.
86
123
util . puts ( 'http proxy server' . blue + ' started ' . green . bold + 'on port ' . blue + '8000' . yellow ) ;
0 commit comments