I have a set of routes. When people goto /v1/addresses/ it does not direct to /v1/addresses. What is the best way to do this with hapi?
module.exports = [
{
method: 'GET',
path: '/v1/addresses',
config: AddressesController.findAll
}, {
method: 'GET',
path: '/v1/addresses/{id}',
config: AddressesController.findById
}, {
method: 'POST',
path: '/v1/addresses',
config: AddressesController.create
}
];
Easiest way should be an event listener:
server.ext('onRequest', function (request, next) {
request.path = request.path.replace(/\/$/, '');
next();
});
How are they getting to /v1/addresses/?
the web its an api endpoint. They are going to https://api.lob.com/v1/addresses/. While I know they should be going to api.lob.com/v1/addresses we have to account for the users that accidently add the /.
Why would users be typing it? I understand developers doing it but that's like typing any other wrong password. If you want to allow trailing slash you can do '/v1/addresses/{p?}' and ignore the value of p. This is a common requirement for optional parameters or navigating directories but not for APIs. It is semantically wrong in HTTP.
Some people build their own clients. We cannot do '/v1/addresses/{p?}' as we have another routes that is '/v1/addresses/{id}'. While it is semantically wrong, when building our product we have to be user friendly. Users expect that when they goto /addresses/ it will be the same as /addresses. If you try any other API out there, this is the default behavior even though its wrong.
Therefore, while wrong we still need to accomodate. Since the API is build on Hapi, we just wanted to figure out the best way to do it.
If you expect others to build public API's on Hapi they will need to do the same thing.
Too many people make the error and add an extra "/" its better to allow it then to make users confused.
There is no simple way to add support for that because it creates a potential conflict in the routing table ('/a', '/a/{p?}' can both match '/a/'). This is why we have not added support for it in the past. I can look into it again but this would be very low priority on my list. The way to handle it today would be to add those routes (with trailing /) manually as redirects to the correct endpoint or just reuse the same route definition.
how about adding an option in the router where trailing slashes getting stripped?
Hmm. That's an interesting idea. Open an new issue for that and I'll think about how it could be done. My concern is how this can create conflicts with routes that end with /.
i guess say most people would want routes that finishes with / and routes that doesn't finish with / to behave the same?
as in, always remove it from the url and always declare the route without it?
@hems The router option has been added (stripTrailingSlash), see the documentation for more details: https://github.com/hapijs/hapi/blob/master/API.md#new-serveroptions
@gergoerdosi works like a charm.
Thanks
:8ball:
This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.
Most helpful comment
@hems The router option has been added (
stripTrailingSlash), see the documentation for more details: https://github.com/hapijs/hapi/blob/master/API.md#new-serveroptions