I also wrote a router, I wanted to get into the different algorithms and
paradigms. Heres my approach:

First i wrote a parser, the parser broke down the path into pieces, and
looked for parameters and regex, it would then classify the route as static
or dynamic. It would then add to a static routing hashtable or a more
complex dynamic routing table.

The router/dispatcher would then start filtering and recursing into the
routing table, until it found a handler. First it would filter by request
method, then try a match on the static hashtable, or fallback to the
dynamic routing table, breaking the url into pieces, recursing into the
table, while extracting possible parameters from the url.

Read this src files:
https://github.com/herenow/light-router/blob/master/lib/routes.js -
Initiates the routing table
https://github.com/herenow/light-router/blob/master/lib/parser.js - Parser
https://github.com/herenow/light-router/blob/master/lib/router.js -
Dispatcher

I think my approach is overly complicated :(

In the end I agree with Ethan, in most cases just use express.

On Wednesday, July 2, 2014, Peter Rust <[email protected]> wrote:

> There's a very simple router here that's just a switch statement with
> regex patterns:
> http://www.idryman.org/blog/2013/08/16/minimal-nodejs-router/
>
> --
> Job board: http://jobs.nodejs.org/
> New group rules:
> https://gist.github.com/othiym23/9886289#file-moderation-policy-md
> Old group rules:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> ---
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected]
> <javascript:_e(%7B%7D,'cvml','nodejs%[email protected]');>.
> To post to this group, send email to [email protected]
> <javascript:_e(%7B%7D,'cvml','[email protected]');>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/nodejs/3a0dfc10-7dd0-4d4a-81cf-209ea9d2c34e%40googlegroups.com
> <https://groups.google.com/d/msgid/nodejs/3a0dfc10-7dd0-4d4a-81cf-209ea9d2c34e%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/CA%2B_%2B6ZaxqabhnNAXjcAGk6DEDW5Evt_88E6jX3n4ZwAkGECG%2BA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to