Catch-All route

44 views
Skip to first unread message

Ori S

unread,
Mar 20, 2017, 2:28:31 PM3/20/17
to RestExpress
Hi,

I guess the Subject says it all: Is it possible in the current implementation to set a default route which will catch all 404 requests?

Would love to know how to do it.

Thanks.

/OS

Todd Fredrich

unread,
May 4, 2017, 4:56:41 PM5/4/17
to RestExpress
Hi Ori,

While there isn't a direct way to have a controller method called when other routes don't match the URL, there is a way to "get in the game" when an exception occurs in the request pipeline. You could possibly do something with that. BUT... that hook is really for formatting error messages. If that particular process throws an exception it would cause some grief--causing the connection to be harshly closed, for example.  For this option, you'd extend the MessageObserver class and override the onException() method. Then when creating your server, add that observer by calling server.addMessageObserver()

Alternatively, you could create a single regex route that matches all URLs for your server that maps to a single controller--then have that controller do the mapping to your other URLs. Kinda clutzy, but it would work.

Hope this helps,
--Todd

Ori S

unread,
May 5, 2017, 2:07:05 AM5/5/17
to RestExpress
Thanks Todd. I like simple solutions so I don't think I'll use any of the above.

Isn't there a nicer way to update RestExpress to support this? If you've got an idea for that, I may be able to code it.

/OS

Todd Fredrich

unread,
May 24, 2017, 1:21:59 PM5/24/17
to RestExpress
As I reread my original response, an alternative to the regex route could really be quite simple. First up, route ordering matters. So...

1) Define the routes per normal (e.g. uri("/foo/bar", controller))
2) As the last route in the list, define a regex route that matches all URLs (e.g. regex("^/.*", catchAllController))
3) Implement all the methods on the catch-all controller to support the various HTTP methods (e.g. create(), read(), update(), delete()).

That should work better and be a bit cleaner than my earlier suggestions...

All the best,
--Todd

Reply all
Reply to author
Forward
0 new messages