hapi — Ignore Trailing Slashes on Route Paths

Future Studio started with just a blog. We’ve used Ghost to write, publish and manage all the tutorials. While extending the blog to a comprehensive learning platform, we chose hapi as the foundational framework. While moving the functionality to show tutorials from Ghost to our own hapi server, we’ve recognized some „strange“ behavior in hapi with request URLs that end on a slash.

In the following, you’ll learn how to properly handle request URLs having a trailing slash.

hapi Series Overview

Problem

In our case, we just noticed this issue due to the fact that Ghost generates all URLs with a trailing slash. While transitioning the functionality to show tutorials from Ghost to our own platform, we —of course— wanted to make sure all URLs out there are still accessible and route to the correct content. And to do that, we need to handle URLs that end on a trailing slash accordingly.

Let’s showcase the problem with a simple example: create a hapi server and register the following two routes.

server.route([  
  {
    method: 'GET',
    path: '/tutorials',
    handler: function (request, reply) {
      reply('Called the /tutorials route, without trailing slash')
    }
  },
  {
    method: 'GET',
    path: '/tutorials/{slug}',
    handler: function (request, reply) {
      reply('Called route with a trailing slash and path param: ' + request.params.name)
    }
  }
 ])

Now call the following URL:

http://localhost:3000/tutorials/  

You’ll notice a 404 error that indicates the given route is not available on your server.

{
  "statusCode":404,
  "error":"Not Found"
}

Well, that’s a bummer. We want to keep the two routes separated, because they are logically different and each deliver completely different content.

The Bad Solution

The „bad solution“ is to just use a single route with an optional path parameter instead of two separate routes. Within the route handler, you differenciate between the two situations: whether a slug is available or not.

server.route(  
  {
    method: 'GET',
    path: '/tutorials/{slug?}',
    handler: function (request, reply) {
      if (!request.params.slug) {
        return reply('Called the /tutorials or /tutorials/ route')
      }

      reply('Called route with slash at the end: ' + request.params.name)
    }
  }
 )

You can immediately spot the problem: hiding functionalities for different routes behind a single route handler will definitely lead to issues. Especially if you’re working in a team there will be complaints by your peers to improve this situation.

We agree, there needs to be a better solution. And actually there is! Read the next section to learn about a clean and sexy way to configure your routes individually and have separated route handlers.

Good Solution

Actually, you can grab the issue directly by its roots. Hapi has built-in functionalities to strip trailing slashes on route paths. That allows you to access URLs with a trailing slash as the pendants without the slash.

The following code block outlines the hapi server configuration to ignore trailing slashes on request URLs:

var Hapi = require('hapi')

// create new server instance
var server = new Hapi.Server()

// add server’s connection information
server.connection({  
  host: 'localhost',
  port: 3000,
  router: {
    stripTrailingSlash: true
  }
})

Using the configuration above, you can split your routes into separate definitions with their individual handler functions. This way, you don’t hide complex processing behind an optional path parameter on a single route.

Add the server configuration from above and two routes from the beginning of this tutorial to your server and start the app. Now visit http://localhost:3000/tutorials/ and see that the trailing slash is ignored by hapi and the correct route handler gets called.

Outlook

The problem of trailing slashes on route paths can be kind of annoying and different platforms generate both URL styles: with and without trailing slashes.

Hapi has built-in support to handle this situation correctly, you just need to activate it in the server’s router configuration.

Hopefully you didn’t go gray from being angry about hapi’s default behavior on trailing slashes!

If you’ve feedback or questions, please don‘t hesitate to reach us on Twitter @futurestud_io or use the comments below. We appreciate any message, success story, or question. Just shout out :)

Make it rock & enjoy coding!

Explore the Library

Find interesting tutorials and solutions for your problems.