1
+ var util = require ( 'util' ) ,
2
+ colors = require ( 'colors' ) ,
3
+ http = require ( 'http' ) ,
4
+ httpProxy = require ( 'http-proxy' ) ,
5
+ Store = require ( './lib/store' )
6
+ //
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.
10
+ //
11
+
12
+ function matcher ( url , dest ) {
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.
20
+ return function ( url ) {
21
+ var m = r ( url )
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 } ;
28
+ }
29
+ }
30
+
31
+ exports . urls = function ( urls ) {
32
+ // This is the entry point for our middleware.
33
+ // 'matchers' is the array of URL matchers, as mentioned above.
34
+ var matchers = [ ] ;
35
+ for ( var url in urls ) {
36
+ // Call the 'matcher' function above, and store the resulting closure.
37
+ matchers . push ( matcher ( url , urls [ url ] ) ) ;
38
+ }
39
+
40
+ // This closure is returned as the request handler.
41
+ return function ( req , res , next ) {
42
+ //
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)
46
+ //
47
+ var proxy = next ;
48
+ for ( var k in matchers ) {
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.
54
+ req . url = m . url ;
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 ) ;
59
+ }
60
+ }
61
+ }
62
+ }
63
+
64
+ http . createServer ( new Store ( ) . handler ( ) ) . listen ( 7531 )
65
+
66
+ // Now we set up our proxy.
67
+ httpProxy . createServer (
68
+ // This is where our middlewares go, with any options desired - in this case,
69
+ // the list of routes/URLs and their destinations.
70
+ exports . urls ( {
71
+ '/store' : { port : 7531 , host : 'localhost' } ,
72
+ '/' : { port : 9000 , host : 'localhost' }
73
+ } )
74
+ ) . listen ( 8000 ) ;
75
+
76
+ //
77
+ // Target Http Server (to listen for requests on 'localhost')
78
+ //
79
+ http . createServer (
80
+ function ( req , res ) {
81
+ res . writeHead ( 200 , { 'Content-Type' : 'text/plain' } ) ;
82
+ res . write ( 'request successfully proxied to: ' + req . url + '\n' + JSON . stringify ( req . headers , true , 2 ) ) ;
83
+ res . end ( ) ;
84
+ } ) . listen ( 9000 ) ;
85
+
86
+ // And finally, some colored startup output.
87
+ util . puts ( 'http proxy server' . blue + ' started ' . green . bold + 'on port ' . blue + '8000' . yellow ) ;
88
+ util . puts ( 'http server ' . blue + 'started ' . green . bold + 'on port ' . blue + '9000 ' . yellow ) ;
0 commit comments