Sort Sails/Express-style route addresses by inclusivity

An algorithm for ordering route addresses

Definition of a "route address"

For the purposes of this algorithm, a route address is a string containing:

  • An optional HTTP verb (or ALL) prefix, followed by a space, followed by:
  • 1 or more path components, where each path component is:
    • A forward slash /, followed by:
    • A colon :followed by any combination of letters and numbers (a "param"), OR
    • A star *(a "wildcard"), OR
    • Any combination of letters and numbers (a "static path component")

In other words, any Sails-compatible route path.


Given an unordered list of route addresses:

  1. Find the maximum number of parts (maxParts) in any address, by ignoring the optional verb prefix and initial forward slash and splitting each address on the remaining /characters.

  2. For each address, construct a string (rank) by examining each part in the address from left to right and:

    • Adding a 1to the string for each static path component
    • Adding a 2to the string for each param component
    • Adding a 3to the string for each wildcard component
    • If the address being examined has fewer than maxPartsparts, pad the string with 0s if no wildcards were encountered, or 4s if wildcards were encountered.
    • Add a 0to the end of the string if the address has a specific verb prefix (i.e. not ALL), and a 1if it has a ALLprefix or no prefix.
    • Finally, prefix a 5at the beginning of the string if the path has nostatic components1.
  3. Sort the addresses by their rank, with lesser ranks coming before greater ones.


----  -----
1000  GET /foo
1001  /foo
1101  /foo/bar
1111  /foo/bar/baz
1120  GET /foo/bar/:baz
1121  /foo/bar/:baz
1130  GET /foo/bar/*
1131  /foo/bar/*
1201  /foo/:bar
1211  /foo/:bar/baz
1221  /foo/:bar/:baz
1311  /foo/*/baz
1341  /foo/*
2101  /:foo/bar
2111  /:foo/bar/baz
2121  /:foo/bar/:baz
2211  /:foo/:bar/baz
3111  /*/bar/baz
3131  /*/baz/*
3141  /*/baz
52001 /:foo
52201 /:foo/:bar
52221 /:foo/:bar/:baz
52231 /:foo/:bar/*
52341 /:foo/*
53440 GET /*
53441 /*

1: This ensures that addresses like /*/baz/*are ranked above ones like /:foo/:bar/:baz-- the static bazcomponent implies that the author intends for */baz/*to handle request URLs of at least three components, one of which is baz; if /:foo/:bar/:baz/were ranked higher in the list, it would swallow the subset of those URLs with exactlythree components.


If you have a question, need professional support, or just want to talk Sails/Node.js with other folks in the community, click here.


To report a bug, click here.


Please observe the guidelines and conventions laid out in the Sails project contribution guidewhen opening issues or submitting pull requests.


Like the Sails framework, this package is free and open-source under the MIT License.




  • (原创)vue-router的Import() 异步加载模块问题的解决方案

    关注不迷路,如果解决了问题,留下个赞。 1、问题现象 (/public/upload/e221e3db24c3f24a41062b6e4e389df8) 2、出现问题的代码点 (/publ...

    22 天前
  • 阵列。sort()没有;T排序数字正确[复制]

    Some Guy(提出了一个问题:Array.sort() doesn't sort numbers co...

    2 年前
  • 重拾vue--vue-router进阶

    路由组件传参 HTML5 History模式 导航守卫 路由元信息 过渡效果 name="router",对应class中的routerenter ...

    1 年前
  • 通过vue-router学习vue

    vuerouter是vue官方支持,可以看做vue最佳实践本文大致梳理vuerouter源码各个文件大体作用,不深入细节建议对照vuerouter源码阅读 src/index.js 1. VueRou...

    2 个月前
  • 通过SortableJS实现elementUItable拖动效果

    背景 目前elmenetUI的表格是不支持拖动的,但表格行拖动要求往往比较常见。通过引入sortableJS可以方便的让elementUI组件中的table支持拖动效果。

    22 天前
  • 详解数组Array.sort()排序的方法

    JavaScript中数组的sort()方法主要用于对数组的元素进行排序。其中,sort()方法有一个可选参数。但是,此参数必须是函数。 数组在调用sort()方法时,如果没有传参将按字母顺序(字符编...

    3 年前
  • 详解使用vue-router进行页面切换时滚动条位置与滚动监听事件


    3 年前
  • 详解react-router 4.0 下服务器如何配合B

    reactrouter作为react框架路由解决方案在react项目中举足轻重。 在reactrouter 4.0版本中,API与先前版本相比有了很大的修改,在2.0、3.0中常用的组件作为路由底层...

    2 年前
  • 记数组sort方法与字符串比较引起的Bug

    前言 前几天使用JavaScript中Array的sort排序字符串,发现排序不准确,这里记一下。 第一版 那就找原因。发现的问题是字符串比较引起的。

    2 年前
  • 让我们来重新设计一下 koa-router

    前言 koarouter 是目前用的比较多的 Koa 的路由中间件之一,前段时间由于作者没有精力继续维护而将其公开售卖(

    1 年前


扫码加入 JavaScript 社区