flatiron / director

a tiny and isomorphic URL router for JavaScript
http://github.com/flatiron/director
MIT License
5.6k stars 486 forks source link

catch all or redirect #335

Open ghost opened 8 years ago

ghost commented 8 years ago

Maybe I have missed the obvious, but how can I implement a catch all route? I currently have a number of routes, but I want that for any url that does not match one of my routes to be caught and sent to a default page.

yongnanzhu commented 8 years ago

you can config router with '/:def' tag like {'/:def': defaultPage}

ghost commented 8 years ago

Currently I am doing:

const router = new director.http.Router({
  '/pws/GetMaterial': {
    get: getMaterial,
  },
  '/pws/AddEvent2': {
    post: genericPost,
  },
});

How would I use '/:def' with this approach, in other words I tried you suggestion and it did not work in the way I am initializing a router.

mhulse commented 7 years ago

A catch all that worked for me:

Router(self.routes())
    .configure({
        on: ns.view.table['all routes']
    })
    .init('/');

… and later in my routing table:

'/?((\w|.)*)': ns.view.table['catch all'], // Catches all non-specified routes.
'/': ns.view.table['splash screen']

https://github.com/flatiron/director/blob/b507baed25cfd38307808ea41a0d6305ee6e1ebf/README.md#wildcard-routes