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)
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.
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.
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.
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.
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.
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:
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:
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.
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.
27 comments
[ 4.1 ms ] story [ 77.1 ms ] thread[1] https://github.com/substack/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.
However I can see a couple of important differences:
1. The parameters are passed to the callback in pagex which makes it cleaner:
While with page.js you have to retrieve them manually: 2. You can negate the url. For instance, if you want something to run in all pages except in the users page: 3. [undocumented, not-official] There is a before and after catch-all which can be useful for debugging, analytics or similar