Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

firstandthird/hapi-trailing-slash

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

81 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

hapi-trailing-slash # Build Status

A hapi plugin that handles common trailing slash issues for incoming URLs, so that my-route and my-route/ will have consistent behaviors.

Installation

npm install hapi-trailing-slash

Basic Use

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 response containing the address of the new route.

  • remove behavior:

    const module = require("hapi-trailing-slash");
    await server.register({
      plugin: module,
      options: {
        method: 'remove'
      }
    });
    server.route({
      method: 'GET',
      path: '/test',
      handler: function (request, h) {
        return 'ok';
      }
    });

    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.

  • append behavior:

    append does the exact opposite of remove:

    const module = require("hapi-trailing-slash");
    await server.register({
      plugin: module,
      options: {
        method: 'append'
      }
    });
    server.route({
      method: 'GET',
      path: '/test/',
      handler: function (request, h) {
        return 'ok';
      }
    });

    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.

You must specify either 'append' or 'remove' when you register the plugin. All other plugin options are optional.

Other Options

  • checkIfExists

    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 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.

    checkIfExists saves your clients from executing a request to a non-existent Location, instead it just tells them that the route does not exist.

  • statusCode

    By default hapi-trailing-slash returns all redirects with a 301 Moved Permanently status code. But you can use this option to specify that you want it to instead use the 302 Found status code (for temporary redirects) when you register the plugin.

  • verbose

    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.