connect-slashes
Advanced tools
Comparing version 0.0.2 to 0.0.9
@@ -7,5 +7,5 @@ /** | ||
* The MIT License | ||
* | ||
* | ||
* Copyright (c) 2010-2012 Roi Avinoam <avinoamr@gmail.com> and connect-slashes authors. | ||
* | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
@@ -17,6 +17,6 @@ * of this software and associated documentation files (the "Software"), to deal | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
@@ -29,14 +29,39 @@ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* THE SOFTWARE. | ||
* | ||
* | ||
*/ | ||
var slashes = function() { | ||
var slashes = function( append ) { | ||
( append === false ) || ( append = true ); // default to append slashes mode | ||
return function( req, res, next ) { | ||
if ( "GET" == req.method && "/" != req.url[ req.url.length - 1 ] ) { | ||
req.url += "/"; | ||
res.writeHead( 301, {'Location': req.url } ); | ||
res.end(); | ||
return; | ||
if ( "GET" == req.method ) { | ||
var url = req.url.split( /[\?\&]+/ ) | ||
, location = url[ 0 ] | ||
, redirect; | ||
if ( append && "/" != location[ location.length - 1 ] ) { | ||
// append slashes | ||
redirect = location + "/"; | ||
} else if ( !append && "/" == location[ location.length - 1 ] && "/" != location ) { | ||
// remove slashes | ||
redirect = location.slice( 0, location.length - 1 ); | ||
} | ||
// complete the redirect url | ||
if ( redirect ) { | ||
if ( url.length > 1 ) { | ||
redirect += "?" + url.slice( 1 ).join( "&" ); | ||
} | ||
res.writeHead( 301, { "Location": redirect } ); | ||
res.end(); | ||
return; | ||
} | ||
} | ||
@@ -43,0 +68,0 @@ next(); |
{ | ||
"name": "connect-slashes" | ||
, "version": "0.0.2" | ||
, "version": "0.0.9" | ||
, "description": "Trailing slash redirect middleware for Connect" | ||
, "keywords": ["trailing", "slash", "connect", "middleware"] | ||
, "author": "Roi Avinoam <avinoamr@gmail.com>" | ||
, "contributors": [ | ||
"Feross Aboukhadijeh <feross@feross.org> (http://feross.org)" | ||
, "brunogfranca (https://github.com/brunogfranca)" | ||
] | ||
, "dependencies": {} | ||
@@ -8,0 +12,0 @@ , "main": "index" |
@@ -15,4 +15,4 @@ connect-slashes | ||
```javascript | ||
var connect = require('connect') | ||
, slashes = require('connect-slashes'); | ||
var connect = require("connect") | ||
, slashes = require("connect-slashes"); | ||
@@ -26,5 +26,11 @@ connect() | ||
Alternatively, you can pass `false` as the only argument to `.slashes()` in order to remove trailing slashes instead of appending them: | ||
```javascript | ||
.use(slashes(false)); | ||
``` | ||
## Notes | ||
1. Only GET requests will be redirected (to avoid losing POST/PUT data) | ||
2. This middleware will append 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 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. |
3816
57
34