connect-slashes
Advanced tools
Comparing version 1.0.2 to 1.2.0
@@ -32,5 +32,6 @@ /** | ||
var reQuery = /[\?\&]+/; | ||
var reAbsolute = /^(\/\/+)/; | ||
var reDoubleSlashes = /(\/\/+)/; | ||
var slashes = function( append ) { | ||
var slashes = function( append, options ) { | ||
options = options || {}; | ||
( append === false ) || ( append = true ); // default to append slashes mode | ||
@@ -43,11 +44,9 @@ | ||
, location = url[ 0 ] | ||
, redirect; | ||
, redirect | ||
, headers; | ||
// Prevent redirect to absolute URLs (see issue #2) | ||
location = location.replace( reAbsolute, "/" ); | ||
if ( append && "/" != location[ location.length - 1 ] ) { | ||
// append slashes | ||
redirect = location + "/"; | ||
redirect = location + "/"; | ||
@@ -57,3 +56,3 @@ } else if ( !append && "/" == location[ location.length - 1 ] && "/" != location ) { | ||
// remove slashes | ||
redirect = location.slice( 0, location.length - 1 ); | ||
redirect = location.slice( 0, location.length - 1 ); | ||
@@ -68,6 +67,15 @@ } | ||
res.writeHead( 301, { "Location": redirect } ); | ||
redirect = ( options.base || "" ) + redirect; // prepend the base path | ||
redirect = redirect.replace( reDoubleSlashes, "/" ); // see issue #2 | ||
if ( "/" != redirect[ 0 ] ) { | ||
redirect = "/" + redirect; // guarantee absolute redirect | ||
} | ||
headers = options.headers || {}; | ||
headers[ "Location" ] = redirect; | ||
res.writeHead( options.code || 301, headers ); | ||
res.end(); | ||
return; | ||
} | ||
@@ -74,0 +82,0 @@ |
{ | ||
"name": "connect-slashes", | ||
"version": "1.0.2", | ||
"description": "Trailing slash redirect middleware for Connect", | ||
"keywords": [ "trailing", "slash", "connect", "middleware" ], | ||
"version": "1.2.0", | ||
"description": "Trailing slash redirect middleware for Connect and Express.js", | ||
"keywords": [ "trailing", "slash", "connect", "middleware", "express" ], | ||
"author": "Roi Avinoam <avinoamr@gmail.com>", | ||
@@ -20,5 +20,5 @@ "contributors": [ | ||
"license": "MIT", | ||
"engines": { | ||
"engines": { | ||
"node": "*" | ||
} | ||
} |
@@ -22,15 +22,35 @@ connect-slashes | ||
.use(slashes()) | ||
.listen(3000); | ||
.listen(3000); | ||
``` | ||
Alternatively, you can pass `false` as the only argument to `.slashes()` in order to remove trailing slashes instead of appending them: | ||
Alternatively, you can pass `false` as the first argument to `.slashes()` in order to remove trailing slashes instead of appending them: | ||
```javascript | ||
.use(slashes(false)); | ||
``` | ||
``` | ||
## Additional settings | ||
You can also pass a second argument with an options object. For example, if an application is behind a reverse proxy server that removes part of the URL (a base_path) before proxying to the application, then the `base` can be specified with an option: | ||
```javascript | ||
.use(slashes(true, { base: "/blog" })); // prepends a base url to the redirect | ||
``` | ||
By default, all redirects are using the 301 Moved Permanently header. You can change this behavior by passing in the optional `code` option: | ||
```javascript | ||
.use(slashes(true, { code: 302 })); // 302 Temporary redirects | ||
``` | ||
You can also set additional headers to the redirect response with the `headers` option: | ||
```javascript | ||
.use(slashes(true, { headers: { "Cache-Control": "public" } })); | ||
``` | ||
## Notes | ||
1. Only GET requests will be redirected (to avoid losing POST/PUT data) | ||
2. This middleware will append or removes a trailing slash to all request urls. This includes filenames (/app.css => /app.css/), so it may break your static files. Make sure to `.use()` this middleware only after the `connect.static()` middleware. | ||
2. This middleware will append or remove a trailing slash to all request urls. This includes filenames (/app.css => /app.css/), so it may break your static files. Make sure to `.use()` this middleware only after the `connect.static()` middleware. | ||
@@ -37,0 +57,0 @@ ## LICENSE |
var slashes = require( ".." ), | ||
assert = require( "assert" ) | ||
assert = require( "assert" ), | ||
http = require( "http" ); | ||
@@ -32,3 +32,3 @@ | ||
assert( "/foo/" == headers.Location ); | ||
}, | ||
}, | ||
end: done | ||
@@ -45,3 +45,3 @@ }, function() { | ||
assert( "/foo" == headers.Location ); | ||
}, | ||
}, | ||
end: done | ||
@@ -57,4 +57,4 @@ }, function() { | ||
writeHead: function( status, headers ) { | ||
assert( status == 301 ) | ||
}, | ||
assert( status == 301 ); | ||
}, | ||
end: done | ||
@@ -67,2 +67,14 @@ }, function() { | ||
// | ||
it( "should control the redirect status code", function( done ) { | ||
slashes( true, { code: 305 } )( { method: "GET", url: "/foo" }, { | ||
writeHead: function( status, headers ) { | ||
assert( status == 305 ); | ||
}, | ||
end: done | ||
}, function() { | ||
assert( false ); // no redirect took place | ||
} ); | ||
}); | ||
// | ||
it( "should forward GET arguments", function( done ) { | ||
@@ -103,3 +115,39 @@ append( { method: "GET", url: "/foo?hello=world&foo=bar" }, { | ||
// | ||
it( "should prepend the base_path argument", function( done ) { | ||
slashes( true, { base: "/foo/" } )( { method: "GET", url: "/bar/world" }, { | ||
writeHead: function( status, headers ) { | ||
assert( "/foo/bar/world/" == headers.Location ); | ||
}, | ||
end: done | ||
}, function() { | ||
assert( false ); // no redirect took place | ||
}); | ||
}); | ||
// | ||
it( "should prepend a first slash", function( done ) { | ||
append( { method: "GET", url: "bar/world" }, { | ||
writeHead: function( status, headers ) { | ||
assert( "/bar/world/" == headers.Location ); | ||
}, | ||
end: done | ||
}, function() { | ||
assert( false ); // no redirect took place | ||
}); | ||
}); | ||
it( "should set headers", function( done ) { | ||
slashes( true, { headers: { "Cache-Control": "public" } } )( { method: "GET", url: "/foo" }, { | ||
writeHead: function( status, headers ) { | ||
assert( "public" == headers["Cache-Control"] ); | ||
assert( "/foo/" == headers.Location ); | ||
}, | ||
end: done | ||
}, function() { | ||
assert( false ); // no redirect took place | ||
} ); | ||
}); | ||
} ); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11171
201
59