News for hackers. Stuff that matters.
Re:Show HN: Simple express-like routing for front-end by olalonde on Wednesday August 17, 2016 @03:12AM (#12302967)
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)
Re:Show HN: Simple express-like routing for front-end by franciscop on Wednesday August 17, 2016 @03:20AM (#12302990)
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 by kevinsimper on Wednesday August 17, 2016 @05:36AM (#12303401)
Why .namespace and not .use like express?
Re:Show HN: Simple express-like routing for front-end by franciscop on Wednesday August 17, 2016 @11:42AM (#12305514)
Because it is completely unrelated to express, and even with express that is not really how it works so it'd only cause confusion
Re:Show HN: Simple express-like routing for front-end by micaksica on Wednesday August 17, 2016 @02:40AM (#12302882)
I'm watching a movie so I haven't looked too deeply at this, but your regexes aren't safe from ReDoS according to substack's safe-regex [1]. It might be worth finding a non-regex-based way to parse regular expressions in case of some weird routes that could cause exponential-time parsing.

[1] https://github.com/substack/safe-regex

Re:Show HN: Simple express-like routing for front-end by franciscop on Wednesday August 17, 2016 @02:50AM (#12302909)
The path is preferred to the Regex, and I hope that no developer is using user input to create a route here. As specified in substack's safe-regex:

> 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.

Re:Show HN: Simple express-like routing for front-end by SwellJoe on Saturday August 20, 2016 @12:26AM (#12325203)
Isn't this client-side? So, what could a malicious user DoS other than their own browser? Am I missing something?
Re:Show HN: Simple express-like routing for front-end by yev on Wednesday August 17, 2016 @04:50AM (#12303233)
Looks good, but isn't it the as existing https://visionmedia.github.io/page.js/ ?
Re:Show HN: Simple express-like routing for front-end by longlho on Wednesday August 17, 2016 @07:57AM (#12303976)
Yeah looks exactly like page.js
Re:Show HN: Simple express-like routing for front-end by yev on Wednesday August 17, 2016 @09:35AM (#12304540)
sorry, "the same as"
Re:Show HN: Simple express-like routing for front-end by longlho on Wednesday August 17, 2016 @08:00AM (#12303989)
Hmm not sure what problem this was trying to solve since client side router has been a solved one for a long time. And this is not looking close to express w/o middleware, which page.js does.
Re:Show HN: Simple express-like routing for front-end by jspdown on Wednesday August 17, 2016 @04:25AM (#12303175)
I don't understand the "express-like" in the title. What's similar to express in your router?
Re:Show HN: Simple express-like routing for front-end by andypants on Wednesday August 17, 2016 @05:15AM (#12303331)
It uses the same path-to-regex package. Other than that I can't tell how it's similar.
Re:Show HN: Simple express-like routing for front-end by urvader on Wednesday August 17, 2016 @02:36AM (#12302869)
Do you plan to support middlewares?
Re:Show HN: Simple express-like routing for front-end by franciscop on Wednesday August 17, 2016 @02:44AM (#12302897)
Could you explain further, please? I plan on changing it a bit internally but not on growing it past its purpose as I like it to do only one thing (and hopefully do it well)
Re:Show HN: Simple express-like routing for front-end by lcarlson on Wednesday August 17, 2016 @03:00AM (#12302930)
I think page.js supports middleware if you're looking for something to emulate. Basically chainring callbacks.
Re:Show HN: Simple express-like routing for front-end by franciscop on Wednesday August 17, 2016 @03:04AM (#12302943)
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 <head> and make it load when the page is ready.
Re:Show HN: Simple express-like routing for front-end by reaktivo on Wednesday August 17, 2016 @03:18AM (#12302981)
A callback is middleware, you can

    page('/some/path', middleware, callback);
Re:Show HN: Simple express-like routing for front-end by franciscop on Wednesday August 17, 2016 @03:23AM (#12302998)
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 by gberger on Wednesday August 17, 2016 @04:43AM (#12303215)
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 by franciscop on Wednesday August 17, 2016 @11:46AM (#12305546)
In the simple way, express accepts a path (url-like fragment) and executes a callback when it matches the url. THAT is what I meant from express-like, but from this and other comments I can see it wasn't clear at all.
Re:Show HN: Simple express-like routing for front-end by lcarlson on Wednesday August 17, 2016 @02:47AM (#12302901)
How is this different from page.js?
Re:Show HN: Simple express-like routing for front-end by franciscop on Wednesday August 17, 2016 @02:57AM (#12302923)
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 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
Re:Show HN: Simple express-like routing for front-end by JasonSage on Wednesday August 17, 2016 @05:54AM (#12303463)
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. :)
Re:Show HN: Simple express-like routing for front-end by franciscop on Wednesday August 17, 2016 @11:58AM (#12305644)
I am totally going to do that, but haven't got around to do it yet. This will also add a bit of flexibility (much needed) about how to pass options.
Re:Show HN: Simple express-like routing for front-end by newname87498324 on Wednesday August 17, 2016 @09:04AM (#12304326)
Honestly one of the worst ShowHN submissions I've ever seen.
Re:Show HN: Simple express-like routing for front-end by franciscop on Wednesday August 17, 2016 @11:51AM (#12305585)
Could you elaborate further, please? What you don't like and what could be improved? It is a small project and I didn't think it'd make it to the front page but yet here it is, so some people must like it and see something positive.