
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
A minimal, composable router that supports sub-routes.
var commuter = require('commuter')
var router = commuter()
router.get('/post/:title', onRoute)
//later, a GET request is made with the url '/post/some-title'
router(req, res)
function onRoute(req, res) {
console.log(req.params.title) // 'some-title'
// handle route...
}
Subroutes work exactly as you'd expect:
var commuter = require('commuter')
var router = commuter()
var subrouter = commuter()
router.get('/post/*', subrouter)
subrouter.get('^/?view/:title', onRoute) // optionally leading slash
// later, a GET request with url '/post/view/some-title'
router(req, res)
function onRoute(req, res) {
console.log(req.params.title) // 'some-title'
console.log(req.url) // '/post/view/some-title'
// handle route...
}
Even handle the "index" route in your subrouter:
var commuter = require('commuter')
var router = commuter()
var subrouter = commuter()
router.get('/post/*', subrouter)
subrouter.get('^$', onRoute) // optionally leading slash
// later, a GET request with url '/post/'
router(req, res)
function onRoute(req, res) {
console.log(req.url) // '/post/'
// handle route...
}
commuter([defaultRoute] [, root] [, verbs])
- Create a new router. Accepts
the following parameters:
defaultRoute
- A function to be called if no routes are matchedroot
- A string to be ignored at the begging of any URL; for example,
passing /some/string
will cause the route /some/string/with/more
to
be matched using only /with/more
verbs
- By default, the standard HTTP verbs are supported: get, post,
put, patch, delete. If you need different verbs, pass them here. It will
replace the defaults, excepting the special any route, which is always
available.The router
that is returned has the following methods:
router(request [, args ...])
- Route a request
through the router. Routes
are matched in the order they were added.
request
- An http.IncomingMessage, as passed by an
http.Server or similar.args
- Any number of arguments which will be passed to the matched
function.router.<method>(pattern, fn)
- Define a route on your router
<method>
- Any of the standard HTTP verbs, or the verbs you defined; for
example, router.get
, router.post
, or router.any
.pattern
- A string that is either a Cucumber-style pattern describing a
URL or a Regex string (not a RegEx object). commuter uses routes
for it's pattern matching, and follows those docs and rules.fn
- The function to be called when your route is matched. This function
should take the same form as your router.<method>
function; that is, if
your router was called as router(req, res)
, your function will be called
with fn(req, res)
The request
object only needs to be "request-like"; that is, the only
properties that are used are request.url
to match the url, and optionally
request.method
, which will default to the router.any
routes if missing.
As the request
passes through the router, a few additional properties are
added to it:
request.params
- A key/value object of the matched parameters from your
pattern, and their captures values.request.splats
- An array of the matched splatsrequest.route
- The last route pattern
that was matched.There are a few other additions that come via routes and are explained in their docs.
MIT. See LICENSE for details.
FAQs
A minimal, composable router that supports sub-routes.
The npm package commuter receives a total of 123 weekly downloads. As such, commuter popularity was classified as not popular.
We found that commuter demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.