@@ -7,55 +7,58 @@ var moment = require('moment');
7
7
8
8
var utils = require ( './utils.js' ) ;
9
9
10
- var Redirect = function ( redirect_data , options ) {
11
-
10
+ var Redirect = function ( redirect_data , options ) {
12
11
options = options || { } ;
13
12
14
13
var redirect = this ;
15
- var server = options . server ;
16
- var router = server . router ;
14
+ var router = options . server . router ;
17
15
var status = redirect_data . permanent ? PERMANENT_STATUS : TEMPORARY_STATUS ;
18
16
var start = redirect_data . start ;
19
17
var end = redirect_data . end ;
20
- var from = redirect_data . from ;
21
- var to = redirect_data . to ;
22
- var expired = ( moment ( ) > moment ( end ) ) ;
23
-
24
- // don't create redirect route at all if expired
25
- // don't check prematurity yet since it could become valid later
26
- if ( ! expired ) {
27
-
28
- if ( from instanceof RegExp ) {
29
- this . route = from ;
30
- } else {
31
- this . route = path . normalize ( from ) . replace ( / \\ / g, '/' ) ;
32
- this . route = utils . formatRouteForExpress ( this . route ) ;
18
+ var from = _ . isObject ( redirect_data . from ) && ! _ . isRegExp ( redirect_data . from ) ? redirect_data . from : { path : redirect_data . from } ;
19
+ var to = _ . isObject ( redirect_data . to ) && ! _ . isFunction ( redirect_data . to ) ? redirect_data . to : { url : redirect_data . to } ;
20
+ var expired = moment ( ) > moment ( end ) ;
21
+
22
+ // Don't create redirect route at all if expired
23
+ // Don't check prematurity yet since it could become valid later
24
+ if ( ! expired ) {
25
+ redirect . route = from . path || '*' ;
26
+ if ( _ . isString ( redirect . route ) ) {
27
+ redirect . route = path . normalize ( redirect . route ) . replace ( / \\ / g, '/' ) ;
28
+ redirect . route = utils . formatRouteForExpress ( redirect . route ) ;
33
29
}
34
30
35
- router . get ( this . route , function ( req , res , next ) {
36
- var expired = ( moment ( ) > moment ( end ) ) ;
37
- var premature = ( moment ( ) < moment ( start ) ) ;
38
- // if redirect is expired or premature skip it
39
- if ( ! expired && ! premature ) {
40
- var url = typeof ( to ) == 'function' ? to ( req . params ) : to ;
41
- res . redirect ( status , utils . expandVariables ( url , req . params ) ) ;
42
- }
43
- else {
44
- next ( ) ;
31
+ router . get ( redirect . route , function ( req , res , next ) {
32
+ // If redirect is expired or premature skip it
33
+ var expired = moment ( ) > moment ( end ) ;
34
+ var premature = moment ( ) < moment ( start ) ;
35
+ if ( expired || premature ) return next ( ) ;
36
+
37
+ // If the protocol or host don't match, skip redirect
38
+ if ( from . protocol && from . protocol !== req . protocol ) return next ( ) ;
39
+ if ( from . host && from . host !== req . host ) return next ( ) ;
40
+
41
+ // Compute to
42
+ var newTo = to ;
43
+ if ( _ . isFunction ( newTo . url ) ) {
44
+ newTo = newTo . url ( req . params ) ;
45
+ if ( ! _ . isObject ( newTo ) ) newTo = Object . assign ( { } , to , { url : newTo } ) ;
45
46
}
46
- } ) ;
47
47
48
- // removes the redirect route
49
- this . destroy = function ( ) {
48
+ // Compute location
49
+ var location = newTo . protocol || newTo . host ? ( ( newTo . protocol || req . protocol ) + '://' + ( newTo . host || req . host ) ) : '' ;
50
+ location += newTo . url ? utils . expandVariables ( newTo . url , req . params ) : req . url ;
50
51
51
- router . routes . get = _ ( router . routes . get ) . reject ( function ( current_route ) {
52
+ res . redirect ( status , location ) ;
53
+ } ) ;
54
+
55
+ // Removes the redirect route
56
+ this . destroy = function ( ) {
57
+ router . routes . get = _ ( router . routes . get ) . reject ( function ( current_route ) {
52
58
return redirect . route === current_route . path ;
53
59
} ) ;
54
-
55
60
} ;
56
-
57
61
}
58
-
59
62
} ;
60
63
61
64
module . exports = Redirect ;
0 commit comments