Live data from Hacker News

Show HN: Simple express-like routing for front-end

github.com

11–20 of 28 posts

Re: Show HN: Simple express-like routing for front-end

#11
post #8

Earlier quoted context omitted.

I think page.js supports middleware if you're looking for something to emulate. Basically chainring callbacks.

Only reference I could find in https://visionmedia.github.io/page.js/ was: 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 and make it load when the page is ready.

A callback is middleware, you can

    page('/some/path', middleware, callback);

Re: Show HN: Simple express-like routing for front-end

#12

One powerful feature of the Express router (but missing here) is that it's just a middleware, making composition trivial, e.g.: const userRouter = router() .get('/', showUser) const usersRouter = router() .get('/', listUsers) .use('/:username', userRouter) const mainRouter = router() .use('/users', usersRouter)

Ah that is really cool, recently a user requested a feature[1] that is basically this but I solved it in a different way [undocumented]:

    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

Re: Show HN: Simple express-like routing for front-end

#13

Earlier quoted context omitted.

Only reference I could find in https://visionmedia.github.io/page.js/ was: 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 and make it load when the page is ready.

A callback is middleware, you can page('/some/path', middleware, callback);

I see, but I don't really like this extra functionality for pagex. I am not looking to copy express with middleware for front-end, just wanted some easy and straight way to load a script if we are in the correct part of the website.

Re: Show HN: Simple express-like routing for front-end

#15

Earlier quoted context omitted.

A callback is middleware, you can page('/some/path', middleware, callback);

I see, but I don't really like this extra functionality for pagex. I am not looking to copy express with middleware for front-end, just wanted some easy and straight way to load a script if we are in the correct part of the website.

Uh, then you and I have very different definitions of "express-like". When I think of express I think of middleware.

Re: Show HN: Simple express-like routing for front-end

#18

One powerful feature of the Express router (but missing here) is that it's just a middleware, making composition trivial, e.g.: const userRouter = router() .get('/', showUser) const usersRouter = router() .get('/', listUsers) .use('/:username', userRouter) const mainRouter = router() .use('/users', usersRouter)

Ah that is really cool, recently a user requested a feature[1] that is basically this but I solved it in a different way [undocumented]: 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/is…

Why .namespace and not .use like express?

Re: Show HN: Simple express-like routing for front-end

#19
post #5

How is this different from page.js?

It was initially a Regex-only project so it was quite different. Then I added path-to-regex and forgot to check if there was some library out there similar so now they are quite similar. 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 retr…

Quick tip, instead of passing true here, having an options object improves readability. As somebody who's never seen it before, the following does the opposite of what I expect:

  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. :)
Post reply on HN