hapi — Optional Path Parameters

Within the tutorial on driving traffic to your website by adding routes to your hapi server, we’ve already touched the basics of path parameters. Within this guide, you’ll get additional information on path parameters, specifically optional ones.

Before diving into the details, have a look at the series outline and find posts that match your interests and needs.

hapi Series Overview

Optional Parameters on Routes

Hapi allows you to define optional path parameters that aren’t required to match a given route’s path. Let’s illustrate this topic using the Future Studio blog as an example. You can view a list of recently published tutorials with a shortened content preview when visiting /blog. For the full tutorial’s content, you need to visit /blog/{tutorial-title-as-slug} where {tutorial-title-as-slug} is the individual title’s slug.

For any route definition that follows below, we’ll use the following base URL:

https://futurestud.io  

You already know that there are at least three required parts for a route definition in your hapi server: method, path, and handler. Your interest is especially geared towards the path where we define an optional path parameter. We’ll use the blog example from above to illustrate how the optional path parameters are defined within the path field:

hapi v17

server.route({  
  method: 'GET',
  path: '/blog/{slug?}',
  handler: (request, h) => {
    const params = request.params || {}

    if (params.slug) {
      // render tutorial content
      return h.view('blog/tutorial-details')
    }

    // render blog overview
    return h.view('blog/overview')
  }
})

hapi v16

server.route({  
  method: 'GET',
  path: '/blog/{slug?}',
  handler: function (request, reply) {
    var params = request.params || {}

    if (params.slug) {
      // render tutorial content
      reply.view('blog/tutorial-details')
    } else {
      // render blog overview
      reply.view('blog/overview')
    }
  }
})

You need to provide a parameter name followed by a question mark encapsulated in cURLy brackets: /{slug?}. Essentially, the question mark tells hapi to match this route for any requests to /blog and /blog/<whatever-comes-here>. The path parameters are parsed by hapi and provided to you as a field on the request object. You can access the object containing path parameters by using request.params.

The path parameter object could look like this:

{
  slug: 'test'
}

Please notice, that the route definition above doesn’t match a request with a URL that has three path segments, like /blog/{slug?}/comments/.

Only Last Named Path Parameter Can Be Optional

You can’t define a route having an optional parameter in the middle of your path. Actually, this restriction is kind of obvious when creating an exemplary route’s path like /blog/{tutorial-title-as-slug?}/comments/{commentId}. Creating a request without providing a {tutorial-title-as-slug}, the URL will look like /blog//comments/123 and that’s an invalid request URL.

Notice: only the last named path parameter can be optional.

Outlook

This guide walked you through the definition of optional path parameters for your routes and also showed you how to access them within your handler functions. When making use of optional path segments, please keep in mind that only the last named parameter can be optional.

We appreciate feedback and love to help you if there’s a question on your mind. Let us know in the comments or on twitter @futurestud_io.

Make it rock & enjoy coding!


Additional Resources

Explore the Library

Find interesting tutorials and solutions for your problems.