GithubHelp home page GithubHelp logo

isabella232 / hapi-trailing-slash Goto Github PK

View Code? Open in Web Editor NEW

This project forked from firstandthird/hapi-trailing-slash

0.0 0.0 0.0 199 KB

Hapi plugin to redirect to non-trailing slash urls

License: MIT License

JavaScript 100.00%

hapi-trailing-slash's Introduction

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.

hapi-trailing-slash's People

Contributors

greenkeeper[bot] avatar jgallen23 avatar orthagonal avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.