Routing {ambiorix} | R Documentation |
Core Routing Class
Description
Core routing class. Do not use directly, see Ambiorix, and Router.
Value
A Routing object.
Public fields
error
Error handler.
Active bindings
basepath
Basepath, read-only.
websocket
Websocket handler.
Methods
Public methods
Method new()
Usage
Routing$new(path = "")
Arguments
path
Prefix path.
Details
Initialise
Method get()
Usage
Routing$get(path, handler, error = NULL)
Arguments
path
Route to listen to,
:
defines a parameter.handler
Function that accepts the request and returns an object describing an httpuv response, e.g.:
response()
.error
Handler function to run on error.
Details
GET Method
Add routes to listen to.
Examples
app <- Ambiorix$new() app$get("/", function(req, res){ res$send("Using {ambiorix}!") }) if(interactive()) app$start()
Method put()
Usage
Routing$put(path, handler, error = NULL)
Arguments
path
Route to listen to,
:
defines a parameter.handler
Function that accepts the request and returns an object describing an httpuv response, e.g.:
response()
.error
Handler function to run on error.
Details
PUT Method
Add routes to listen to.
Method patch()
Usage
Routing$patch(path, handler, error = NULL)
Arguments
path
Route to listen to,
:
defines a parameter.handler
Function that accepts the request and returns an object describing an httpuv response, e.g.:
response()
.error
Handler function to run on error.
Details
PATCH Method
Add routes to listen to.
Method delete()
Usage
Routing$delete(path, handler, error = NULL)
Arguments
path
Route to listen to,
:
defines a parameter.handler
Function that accepts the request and returns an object describing an httpuv response, e.g.:
response()
.error
Handler function to run on error.
Details
DELETE Method
Add routes to listen to.
Method post()
Usage
Routing$post(path, handler, error = NULL)
Arguments
path
Route to listen to.
handler
Function that accepts the request and returns an object describing an httpuv response, e.g.:
response()
.error
Handler function to run on error.
Details
POST Method
Add routes to listen to.
Method options()
Usage
Routing$options(path, handler, error = NULL)
Arguments
path
Route to listen to.
handler
Function that accepts the request and returns an object describing an httpuv response, e.g.:
response()
.error
Handler function to run on error.
Details
OPTIONS Method
Add routes to listen to.
Method all()
Usage
Routing$all(path, handler, error = NULL)
Arguments
path
Route to listen to.
handler
Function that accepts the request and returns an object describing an httpuv response, e.g.:
response()
.error
Handler function to run on error.
Details
All Methods
Add routes to listen to for all methods GET
, POST
, PUT
, DELETE
, and PATCH
.
Method receive()
Usage
Routing$receive(name, handler)
Arguments
name
Name of message.
handler
Function to run when message is received.
Details
Receive Websocket Message
Examples
app <- Ambiorix$new() app$get("/", function(req, res){ res$send("Using {ambiorix}!") }) app$receive("hello", function(msg, ws){ print(msg) # print msg received # send a message back ws$send("hello", "Hello back! (sent from R)") }) if(interactive()) app$start()
Method print()
Usage
Routing$print()
Details
Method engine()
Usage
Routing$engine(engine)
Arguments
engine
Engine function.
Details
Engine to use for rendering templates.
Method use()
Usage
Routing$use(use)
Arguments
use
Either a router as returned by Router, a function to use as middleware, or a
list
of functions. If a function is passed, it must accept two arguments (the request, and the response): this function will be executed every time the server receives a request. Middleware may but does not have to return a response, unlike other methods such asget
Note that multiple routers and middlewares can be used.
Details
Use a router or middleware
Method get_routes()
Usage
Routing$get_routes(routes = list(), parent = "")
Arguments
routes
Existing list of routes.
parent
Parent path.
Details
Get the routes
Method get_receivers()
Usage
Routing$get_receivers(receivers = list())
Arguments
receivers
Existing list of receivers
Details
Get the websocket receivers
Method get_middleware()
Usage
Routing$get_middleware(middlewares = list(), parent = "")
Arguments
middlewares
Existing list of middleswares
parent
Parent path
Details
Get the middleware
Method prepare()
Usage
Routing$prepare()
Details
Prepare routes and decomposes paths
Method clone()
The objects of this class are cloneable with this method.
Usage
Routing$clone(deep = FALSE)
Arguments
deep
Whether to make a deep clone.
Examples
## ------------------------------------------------
## Method `Routing$get`
## ------------------------------------------------
app <- Ambiorix$new()
app$get("/", function(req, res){
res$send("Using {ambiorix}!")
})
if(interactive())
app$start()
## ------------------------------------------------
## Method `Routing$receive`
## ------------------------------------------------
app <- Ambiorix$new()
app$get("/", function(req, res){
res$send("Using {ambiorix}!")
})
app$receive("hello", function(msg, ws){
print(msg) # print msg received
# send a message back
ws$send("hello", "Hello back! (sent from R)")
})
if(interactive())
app$start()