| Title: | 'Express.js' Like Routing for R Web Frameworks |
|---|---|
| Description: | It aims to provide R web frameworks a routing mechanism of HTTP requests inspired by the battle tested 'Express.js' web framework. |
| Authors: | Julio Collazos [aut, cre] (ORCID: <https://orcid.org/0009-0006-5503-0997>), router contributors [ctb, cph] (router contributors; authors listed in <https://github.com/pillarjs/router/graphs/contributors>) |
| Maintainer: | Julio Collazos <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.1.1 |
| Built: | 2026-10-04 10:14:16 UTC |
| Source: | https://github.com/juliocollazos64/routing |
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.
No error and no response sent – writes a 404 Not Found HTML
response. The body message is "Cannot <METHOD> <PATH_INFO>".
Error – derives the HTTP status from err$status (must be a
numeric 4xx or 5xx value); falls back to res$status, and then to
500. The body contains the standard HTTP phrase for the status code.
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.
finalHandler(req, res, options)finalHandler(req, res, options)
req |
( |
res |
( |
options |
( |
A function(err) that sends the final HTTP response.
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.
forward() instead of next() – next is a reserved word in R.
forward("route") and forward("router") work identically to their
Express counterparts.
forward is implicit – handlers do not need to declare forward as
an argument to call it; it is automatically injected into the handler's
formals if absent. function(req, res) { forward() } works just as well
as function(req, res, forward) { forward() }.
forward is auto-called – if a handler returns without calling
forward() or sending a response, forward() is called automatically.
Error handlers take three arguments – write error handlers as
(err, req, res); forward is injected automatically as the fourth
argument.
$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.
Router$new()Creates a new Router.
Router$new(caseSensitive = FALSE, mergeParams = FALSE, strict = FALSE)
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.
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.
Router$handle(req, res = NULL, callback = NULL)
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.
Router$use(...)
...(function | list)
An optional leading character(1) path prefix, followed by one or more
handler functions, nested lists of functions, or another Router.
self invisibly.
Router$route()Creates a new Route for path and appends it to the stack.
Router$route(path)
path(character(1))
Path pattern.
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):
value – the captured value of the parameter.
name – the parameter name (character).
Router$param(name, fn)
name(character(1))
Name of the route parameter to watch.
fn(function)
Callback with signature function(req, res, value, name).
self invisibly.
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.
Router$static(root, url, ...)
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().
self invisibly.
Router$getStack()Returns the internal layer stack.
Router$getStack()
list of Layer objects.
Router$clone()The objects of this class are cloneable with this method.
Router$clone(deep = FALSE)
deepWhether to make a deep clone.
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)) })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)) })