Show HN: Simple express-like routing for front-end
github.com
github.com
const userRouter = router()
.get('/', showUser)
const usersRouter = router()
.get('/', listUsers)
.use('/:username', userRouter)
const mainRouter = router()
.use('/users', usersRouter) pagex.base = '/users';
pagex('/', listUsers);
pagex('/:username', showUser);
pagex.base = '';
I am still considering extend it in this way: pagex.namespace('/users', function(pagex){
pagex('/', listUsers);
pagex('/:username', showUser);
});
[1] https://github.com/franciscop/pagex/issues/1> WARNING: This module merely seems to work given all the catastrophic regular expressions I could find scouring the internet, but I don't have enough of a background in automata to be absolutely sure that this module will catch all exponential-time cases.
I also don't have it and I think the case for this error is so contrieved that it doesn't make sense to add a check in pagex. So adding a couple of warnings, one for not trusting user input and another for this specific error, should be fine.
page(callback)
This is equivalent to page('*', callback) for generic "middleware".
Which is... pointless? If you are going to call a function everywhere in front-end why don't you use an IIFE for scope or just nothing at all? Unless you wanted to put it on the <head> and make it load when the page is ready. page('/some/path', middleware, callback);However I can see a couple of important differences:
1. The parameters are passed to the callback in pagex which makes it cleaner:
pagex('/users/:id/:frag?', function(id, frag = 'profile'){ ... });
While with page.js you have to retrieve them manually: pagex('users/:id/:frag?', function(ctx){ var id = ctx[0], frag = ctx[1] });
2. You can negate the url. For instance, if you want something to run in all pages except in the users page: pagex('/users', true, function(){ ... });
3. [undocumented, not-official] There is a before and after catch-all which can be useful for debugging, analytics or similar pagex('/users', true, function(){ ... });
But _this_ is more readable—communicates exactly what I need to know, and requires no previous knowledge of the library: pagex('/users', { negate: true }, function(){ ... });
Thanks for sharing your library, I love how you've put the ergonomics of use first. :)