Package {routing}


Title: 'Express.js' Like Routing for R Web Frameworks
Version: 1.1.1
Description: It aims to provide R web frameworks a routing mechanism of HTTP requests inspired by the battle tested 'Express.js' web framework.
License: MIT + file LICENSE
Encoding: UTF-8
Imports: httpcode, pater, promises, R6, utils
Suggests: mochita, testthat (≥ 3.0.0), yyjsonr
Config/testthat/edition: 3
URL: https://github.com/JulioCollazos64/routing
BugReports: https://github.com/JulioCollazos64/routing/issues
Config/roxygen2/version: 8.0.0
NeedsCompilation: no
Packaged: 2026-09-04 09:46:09 UTC; julio
Author: Julio Collazos ORCID iD [aut, cre], router contributors [ctb, cph] (router contributors; authors listed in <https://github.com/pillarjs/router/graphs/contributors>)
Maintainer: Julio Collazos <amarullazo626@gmail.com>
Repository: CRAN
Date/Publication: 2026-09-04 10:00:02 UTC

Router

Description

A port of the pillarjs/router package for R. Maintains an ordered stack of layers; each layer pairs a path pattern with a middleware function, a nested Router, or a Route object. Middleware and nested routers are added via ⁠$use()⁠; routes are added via ⁠$route()⁠ or the HTTP-verb shortcuts. When a request arrives, the stack is walked in order and each matching layer is invoked until the request is handled or the stack is exhausted.

Important details

HTTP verb shortcuts

⁠$get()⁠, ⁠$post()⁠, ⁠$put()⁠, ⁠$delete()⁠, etc. (one per HTTP verb) and ⁠$all()⁠ are convenience wrappers with signature ⁠(path, ...)⁠ equivalent to ⁠router$route(path)$<verb>(...)⁠. They return self invisibly for chaining.

Methods

Public methods


Router$new()

Creates a new Router.

Usage
Router$new(caseSensitive = FALSE, mergeParams = FALSE, strict = FALSE)
Arguments
caseSensitive

(logical(1))
When TRUE, path matching is case-sensitive (⁠/Foo⁠ does not match ⁠/foo⁠). Default FALSE.

mergeParams

(logical(1))
When TRUE, req$params from a parent router are merged with those of this router instead of being replaced. Default FALSE.

strict

(logical(1))
When TRUE, trailing slashes are significant (⁠/foo/⁠ does not match ⁠/foo⁠). Default FALSE.

Returns

A new Router object.


Router$handle()

Dispatches a request through the router's layer stack. Normally called by a server or a parent router rather than directly.

Usage
Router$handle(req, res = NULL, callback = NULL)
Arguments
req

(environment)
Rook request environment.

res

(Response)
Response object.

callback

(function)
Called when no layer matched or an unhandled error occurred.


Router$use()

Mounts one or more middleware handlers, optionally scoped to a path prefix. A Router may be passed and will be wrapped automatically. Functions whose first parameter is named err are treated as error handlers.

Usage
Router$use(...)
Arguments
...

(⁠function | list⁠)
An optional leading character(1) path prefix, followed by one or more handler functions, nested lists of functions, or another Router.

Returns

self invisibly.


Router$route()

Creates a new Route for path and appends it to the stack.

Usage
Router$route(path)
Arguments
path

(character(1))
Path pattern.

Returns

The new Route invisibly.


Router$param()

Registers a callback triggered whenever a named route parameter is present in a matched route. The callback runs before the route handler.

The callback signature is ⁠function(req, res, value, name)⁠:

Usage
Router$param(name, fn)
Arguments
name

(character(1))
Name of the route parameter to watch.

fn

(function)
Callback with signature ⁠function(req, res, value, name)⁠.

Returns

self invisibly.

Examples
router <- Router$new()

router$param("id", function(req, res, value, name) {
  user <- findUser(value)
  req$user <- list(id = value, name = user$name)
  forward()
})

router$get("/user/:id", function(req, res) {
  res$send(paste("user:", req$user$name))
})

Router$static()

Registers a directory of static files to be served at a given URL prefix. Static files are served directly by httpuv's background I/O thread without invoking R.

Usage
Router$static(root, url, ...)
Arguments
root

(character(1))
Local filesystem path to the directory containing the files to serve.

url

(character(1))
URL prefix at which the files will be available (e.g. "/static").

...

Additional arguments passed to httpuv::staticPath().

Returns

self invisibly.


Router$getStack()

Returns the internal layer stack.

Usage
Router$getStack()
Returns

list of Layer objects.


Router$clone()

The objects of this class are cloneable with this method.

Usage
Router$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

Examples

router <- Router$new()

router$get(
  "/get",
  function(req, res) {
    res$send("Hello there!")
  }
)$post(
  "/post",
  function(req, res) {
    res$send("Goodbye!")
  }
)

router$get(
  "/hello",
  function(req, res) {
    forward()
  },
  function(req, res) {
    res$send("Hello!")
  }
)

router$post("/bye", function(req, res) {
  res$send("Bye!")
})

router$route("/hi")$get(function(req, res) {
  res$send("handling a GET request!")
})$post(function(req, res) {
  res$send("handling a POST request!")
})

router$get(
  c("/path", "/another-path"),
  function(req, res) {
    res$send("Hola!")
  }
)

## ------------------------------------------------
## Method `Router$param()`
## ------------------------------------------------

router <- Router$new()

router$param("id", function(req, res, value, name) {
  user <- findUser(value)
  req$user <- list(id = value, name = user$name)
  forward()
})

router$get("/user/:id", function(req, res) {
  res$send(paste("user:", req$user$name))
})

Final Handler

Description

Returns a ⁠function(err)⁠ to be used as the callback argument of Router⁠$handle()⁠. It is invoked when the router exhausts its stack without sending a response, or when an unhandled occurs.

Behaviour

In all cases the response is an HTML document and includes the security headers ⁠Content-Security-Policy: default-src 'none'⁠ and X-Content-Type-Options: nosniff. Pre-existing Content-Encoding, Content-Language, and Content-Range headers are removed.

Usage

finalHandler(req, res, options)

Arguments

req

(environment)
Rook request environment.

res

(Response)
Response object.

options

(list)
Reserved for future use; currently ignored.

Value

A ⁠function(err)⁠ that sends the final HTTP response.


Build a matcher

Description

Build a matcher

Usage

matcher(path, opts)

Arguments

path

A layer path.

opts

Arguments passed to pater::match().

Value

A function to match against request paths.

mirror server hosted at Truenetwork, Russian Federation.