Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Method wildcard option does not work #1212

Closed
kpdecker opened this issue Dec 14, 2013 · 5 comments
Closed

Method wildcard option does not work #1212

kpdecker opened this issue Dec 14, 2013 · 5 comments
Labels
non issue Issue is not a problem or requires changes

Comments

@kpdecker
Copy link
Contributor

var Hapi = require('hapi');

var server = Hapi.createServer('localhost', 8000);

server.route({
  method: '*',
  path: '/log',
  handler: function(request, reply) {
    reply({foo: 'bar'});
  }
});

server.start();

Returns a 404 error when curled.

hueniverse pushed a commit that referenced this issue Dec 14, 2013
@hueniverse
Copy link
Contributor

Not seeing it.

@kpdecker
Copy link
Contributor Author

This must be the missing piece.


server.route({
  method: 'GET',
  path: '/{path*}',
  handler: {
    directory: {
      path: 'static'
    }
  }
});

An explicit GET handler exists generically and is given precedence. Makes more sense now but seems less usable as the GET handler needs to go to * or need to expand out the log handler for each method that needs to be supported.

@hueniverse
Copy link
Contributor

Literal verbs get precedence over wildcards:

https://github.com/spumko/hapi/blob/master/lib/router.js#L49-L56

I agree this is not ideal but not sure how to adjust it. Going back and forth between wildcard and verb-specific will degrade performance for the much more common use cases. It will require looking in both tables and if two matches found, comparing their paths for the more specific one.

I am inclined to simply document this and suggest you avoid using '*', but instead explicitly add the routes you need.

@kpdecker
Copy link
Contributor Author

I think that is fair if documented.

Would you consider allowing an array of methods for the method parameter? Effectively would be syntax sugar for whatever construct that you would need to do to otherwise reduce code duplication if you need to support multiple methods for the same route and handler.

I.e.

server.route({
  method: ['GET', 'POST'],
  path: '/log',
  handler: function(request, reply) {
    reply({foo: 'bar'});
  }
});

@hueniverse
Copy link
Contributor

Done.

jmonster pushed a commit to jmonster/hapi that referenced this issue Feb 10, 2014
@lock lock bot locked as resolved and limited conversation to collaborators Jan 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
non issue Issue is not a problem or requires changes
Projects
None yet
Development

No branches or pull requests

2 participants