Skip to content

Commit 40f3c5d

Browse files
authored
Merge pull request #23 from firstandthird/readme
Update README.md
2 parents 88bed4c + ef1c6ec commit 40f3c5d

File tree

1 file changed

+72
-19
lines changed

1 file changed

+72
-19
lines changed

README.md

Lines changed: 72 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,77 @@
11
# hapi-trailing-slash # [![Build Status](https://travis-ci.org/firstandthird/hapi-trailing-slash.svg?branch=master)](https://travis-ci.org/firstandthird/hapi-trailing-slash)
22

3-
## Handles common trailing slash issues for incoming URLs ##
4-
5-
###register like so:###
6-
7-
```javascript
8-
var module = require("hapi-trailing-slash");
9-
.
10-
.
11-
.
12-
server.register({
13-
register: module,
14-
options: {
15-
method: 'remove',
16-
verbose: true
17-
}
18-
});
3+
A [hapi](https://hapi.dev/) plugin that handles common trailing slash issues for incoming URLs,
4+
so that _my-route_ and _my-route/_ will have consistent behaviors.
5+
6+
## Installation
7+
8+
```
9+
npm install hapi-trailing-slash
1910
```
2011

21-
###options are:###
12+
## Basic Use
13+
14+
This plugin will register a preResponse extension that checks the path of all 404 Not Found results before they are returned to the client. Depending on the behavior you've requested, it will either add or remove a trailing '/' to the request as needed and then return either a permanent or temporary [HTTP Redirect](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/301) response containing the address of the new route.
15+
16+
- __remove__ behavior:
17+
18+
```javascript
19+
const module = require("hapi-trailing-slash");
20+
await server.register({
21+
plugin: module,
22+
options: {
23+
method: 'remove'
24+
}
25+
});
26+
server.route({
27+
method: 'GET',
28+
path: '/test',
29+
handler: function (request, h) {
30+
return 'ok';
31+
}
32+
});
33+
```
34+
35+
Since this specifies the 'remove' behavior, if you try to GET _/test/_ (which contains an extra trailing slash and thus does not match the route path), hapi-trailing-slash will intercept the 404 before it is returned. It will instead return an HTTP Redirect response with the Location header set to _/test_. And you can still GET _/test_ directly and the plugin will not interfere.
36+
37+
- __append__ behavior:
38+
39+
_append_ does the exact opposite of _remove_:
40+
41+
```javascript
42+
const module = require("hapi-trailing-slash");
43+
await server.register({
44+
plugin: module,
45+
options: {
46+
method: 'append'
47+
}
48+
});
49+
server.route({
50+
method: 'GET',
51+
path: '/test/',
52+
handler: function (request, h) {
53+
return 'ok';
54+
}
55+
});
56+
```
57+
58+
Now if you try to GET _/test_ (which does not match the route path), hapi-trailing-slash will intercept the call before the 404 Not Found error is returned to the client. It then adds a trailing slash and instead returns an HTTP Redirect with the Location set to _/test/_. But GET _/test/_ will still work as normal and you'll get the expected 'ok' response.
59+
60+
61+
You must specify either 'append' or 'remove' when you register the plugin. All other plugin options are optional.
62+
63+
## Other Options
64+
65+
- __checkIfExists__
66+
67+
By default hapi-trailing-slash will not check if your new route exists before automatically returning a redirect to it. But if you set _checkIfExists_ to true, then the plugin will do a quick [HTTP HEAD](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD) check to see if the new route actually exists. If it does not get a response, then it will go ahead and just return the original 404 Not Found result instead of a Redirect.
68+
69+
_checkIfExists_ saves your clients from executing a request to a non-existent Location, instead it just tells them that the route does not exist.
70+
71+
- __statusCode__
72+
73+
By default hapi-trailing-slash returns all redirects with a [301 Moved Permanently](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/301) status code. But you can use this option to specify that you want it to instead use the [302 Found](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/302) status code (for temporary redirects) when you register the plugin.
74+
75+
- __verbose__
2276
23-
* 'append' -- detects incoming requests that have no trailing slash, adds one, and redirects to the new url
24-
* 'remove' -- detects incoming requests with a trailing slash, removes it, and redirects to the new url
77+
Set this to _true_ to have hapi-trailing-slash post a server log of all redirects that it executes. This will include info like the user agent, browser, and from/to request paths. Default is false.

0 commit comments

Comments
 (0)