routing is an R port of pillarjs/router, the
routing mechanism that underpins Express.js. It gives R web frameworks a
syntax that mimics how Express.js applications are written, so the same
patterns that make Express.js applications easy to read and reason about
are now available in R.
pak::pak("JulioCollazos64/routing")Let’s look at an example:
library(routing)
router <- Router$new()
router$use(function(req,res){
print(req)
})
router$get("/hello", function(req,res){
res$send("Hello world!")
})Even if we knew nothing about web development, we can clearly see the things that matter from this code snippet:
router that holds all the rules for how incoming requests
should be handled.router$use() appends a
piece of code that runs on every requests, regardless of path or
method.get means this route only
responds to HTTP GET requests/hello is the URL path this
route matchesMoreover, the routing mechanism happens in the same order you write your code so you can read how your request goes from top to bottom.
forward() instead of next() to pass
control to the next handler.forward as an argument to
your handler to call it inside.forward(), one is called on your behalf.