1
1
/*
2
2
node-http-proxy.js: Lookup table for proxy targets in node.js
3
3
4
- Copyright (c) 2010 Charlie Robbins
4
+ Copyright (c) 2010 Charlie Robbins
5
5
6
6
Permission is hereby granted, free of charge, to any person obtaining
7
7
a copy of this software and associated documentation files (the
@@ -29,39 +29,42 @@ var util = require('util'),
29
29
fs = require ( 'fs' ) ;
30
30
31
31
//
32
- // ### function ProxyTable (router, silent)
32
+ // ### function ProxyTable (router, silent)
33
33
// #### @router {Object} Object containing the host based routes
34
34
// #### @silent {Boolean} Value indicating whether we should suppress logs
35
35
// #### @hostnameOnly {Boolean} Value indicating if we should route based on __hostname string only__
36
36
// Constructor function for the ProxyTable responsible for getting
37
37
// locations of proxy targets based on ServerRequest headers; specifically
38
38
// the HTTP host header.
39
39
//
40
- var ProxyTable = exports . ProxyTable = function ( router , silent , hostnameOnly ) {
40
+ var ProxyTable = exports . ProxyTable = function ( options ) {
41
41
events . EventEmitter . call ( this ) ;
42
-
43
- this . silent = typeof silent !== 'undefined' ? silent : true ;
44
- this . hostnameOnly = typeof hostnameOnly !== 'undefined' ? hostnameOnly : false ;
45
-
46
- if ( typeof router === 'object' ) {
42
+
43
+ this . silent = options . silent || options . silent !== true ;
44
+ this . hostnameOnly = options . hostnameOnly === true ;
45
+
46
+ if ( typeof options . router === 'object' ) {
47
47
//
48
- // If we are passed an object literal setup
49
- // the routes with RegExps from the router
48
+ // If we are passed an object literal setup
49
+ // the routes with RegExps from the router
50
50
//
51
- this . setRoutes ( router ) ;
51
+ this . setRoutes ( options . router ) ;
52
52
}
53
- else if ( typeof router === 'string' ) {
53
+ else if ( typeof options . router === 'string' ) {
54
54
//
55
- // If we are passed a string then assume it is a
55
+ // If we are passed a string then assume it is a
56
56
// file path, parse that file and watch it for changes
57
57
//
58
58
var self = this ;
59
- this . routeFile = router ;
60
- this . setRoutes ( JSON . parse ( fs . readFileSync ( router ) ) . router ) ;
61
-
59
+ this . routeFile = options . router ;
60
+ this . setRoutes ( JSON . parse ( fs . readFileSync ( options . router ) ) . router ) ;
61
+
62
62
fs . watchFile ( this . routeFile , function ( ) {
63
63
fs . readFile ( self . routeFile , function ( err , data ) {
64
- if ( err ) throw err ;
64
+ if ( err ) {
65
+ self . emit ( 'error' , err ) ;
66
+ }
67
+
65
68
self . setRoutes ( JSON . parse ( data ) . router ) ;
66
69
self . emit ( 'routes' , self . hostnameOnly === false ? self . routes : self . router ) ;
67
70
} ) ;
@@ -78,17 +81,17 @@ var ProxyTable = exports.ProxyTable = function (router, silent, hostnameOnly) {
78
81
util . inherits ( ProxyTable , events . EventEmitter ) ;
79
82
80
83
//
81
- // ### function setRoutes (router)
84
+ // ### function setRoutes (router)
82
85
// #### @router {Object} Object containing the host based routes
83
- // Sets the host-based routes to be used by this instance.
86
+ // Sets the host-based routes to be used by this instance.
84
87
//
85
88
ProxyTable . prototype . setRoutes = function ( router ) {
86
89
if ( ! router ) {
87
90
throw new Error ( 'Cannot update ProxyTable routes without router.' ) ;
88
91
}
89
-
92
+
90
93
this . router = router ;
91
-
94
+
92
95
if ( this . hostnameOnly === false ) {
93
96
var self = this ;
94
97
this . routes = [ ] ;
@@ -105,7 +108,7 @@ ProxyTable.prototype.setRoutes = function (router) {
105
108
} ;
106
109
107
110
//
108
- // ### function getProxyLocation (req)
111
+ // ### function getProxyLocation (req)
109
112
// #### @req {ServerRequest} The incoming server request to get proxy information about.
110
113
// Returns the proxy location based on the HTTP Headers in the ServerRequest `req`
111
114
// available to this instance.
@@ -114,14 +117,14 @@ ProxyTable.prototype.getProxyLocation = function (req) {
114
117
if ( ! req || ! req . headers || ! req . headers . host ) {
115
118
return null ;
116
119
}
117
-
120
+
118
121
var target = req . headers . host . split ( ':' ) [ 0 ] ;
119
122
if ( this . hostnameOnly == true ) {
120
123
if ( this . router . hasOwnProperty ( target ) ) {
121
124
var location = this . router [ target ] . split ( ':' ) ,
122
125
host = location [ 0 ] ,
123
126
port = location . length === 1 ? 80 : location [ 1 ] ;
124
-
127
+
125
128
return {
126
129
port : port ,
127
130
host : host
@@ -131,7 +134,9 @@ ProxyTable.prototype.getProxyLocation = function (req) {
131
134
else {
132
135
target += req . url ;
133
136
for ( var i in this . routes ) {
134
- var match , route = this . routes [ i ] ;
137
+ var route = this . routes [ i ] ,
138
+ match ;
139
+
135
140
if ( match = target . match ( route . route ) ) {
136
141
var location = route . target . split ( ':' ) ,
137
142
host = location [ 0 ] ,
@@ -144,7 +149,7 @@ ProxyTable.prototype.getProxyLocation = function (req) {
144
149
}
145
150
}
146
151
}
147
-
152
+
148
153
return null ;
149
154
} ;
150
155
0 commit comments