hapi — Multi-Segment/Wildcard Path Parameters

Last week’s tutorial on hapi walked you through optional path parameters and showed you how to specify routes leveraging optional parameters in their path. Within this week’s guide you’ll learn about route definitions using wildcard path parameters to match routes with multiple segments.

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

hapi Series Overview

Wildcard Parameters on Routes

Besides optional path parameters, you can make use of wildcard parameters for your route specifications that allow multiple path parameter segments. Each segment is divided by a forward slash /.

On the hapi route definition, wildcard parameters are indicated by using the asterisk * character followed by a number at the end of defined parameter name.

hapi v17

server.route({  
  method: 'GET',
  path: '/js/{file*}',
  handler: (request, h) => {
    const params = request.params

    return params
  }
})

hapi v16

server.route({  
  method: 'GET',
  path: '/js/{file*}',
  handler: function (request, reply) {
    var params = request.params

    reply(params)
  }
})

Actually, you can omit the number of segments and hapi will match any available number of segments (like we did in the example above). We’ll have a detailed look at defining multiple segments in the section below.

hapi parses the provided path parameters within the request URL and provides access
to them within an object that is available via request.params.

For now, let’s visualize the usage of wildcard parameters based on the code snippet from above and the following base URL:

https://futurstud.io/  

The code example above indicates that the route will serve JS files. A request to the URL

https://futurstud.io/js/test.js  

will result in the following parsed path parameter object:

{
  file: 'test.js'
}

You might ask yourself, what if I’m going to request a JS file that has a “nested” location within your project and is not at the same level like the test.js file? How would I define the route? Actually, your route is already defined :)

The wildcard route as you’ve defined like in the code block above already matches requests with multiple segments. hapi provides the value for your parameter name (here file) as a complete string.

Imagine a request to

https://futurestud.io/js/vuejs/vue.min.js  

will result in the parsed path parameter values of

{
  file: 'vuejs/vue.min.js'
}

You can read more about serving static files with hapi within the previously published tutorial in this hapi series.

Multi-Segment (Wildcard) Parameters

If you expect a fixed amount of parameters for a given route, you can define them using a number after the asterisk.

hapi v17

server.route({  
  method: 'GET',
  path: '/filter/{type*2}',
  handler: (request, reply) => {
    // this route matches only if 2 path segments are provided after "filter"
    const params = request.params

    return params
  }
})

hapi v16

server.route({  
  method: 'GET',
  path: '/filter/{type*2}',
  handler: function (request, reply) {
    // this route matches only if 2 path segments are provided after "filter"
    var params = request.params

    reply(params)
  }
})

Be aware that the route above only matches on requests to /filter/<first-value>/<second-value>. If you provide only a single value or more than two values, hapi won’t match this definition.

Let’s illustrate how the path parameters will look if your request URL looks like this:

https://futurestud.io/filter/video/premium  

Results in a path parameter object like this:

{
  type: 'video/premium'
}

Notice that hapi will keep the forward slash / within the parameter string. You can get the individual values by using request.params.type.split('/') and access them within the resulting array. Adjust the manipulation of your received parameters to your needs :)

Only Last Named Path Parameter Can Be Optional

You can’t define a route having a wildcard parameter in the middle of your path. Actually, this restriction is kind of obvious when creating an exemplary route’s path like /filter/{type*2}/comments/{commentId}. Creating a request without providing two segments for type, the URL will look like /filter///comments/123 and that’s an invalid request URL.

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

Also, there’s no fallback solution or default values for wildcard parameters. Meaning, you can’t work around hapi routes to make use of optional path parameters using predefined values.

Outlook

Hopefully this guide shows you the advantages of wildcard parameters and how to apply them for multiple segments. Please keep in mind that hapi only allows the last named parameter to be optional and that applies to wildcard parameters as well.

At this point you know the fundamentals on how to handle incoming data with requests. Next stop: how to validate incoming data within the request payload using the joi library.

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.