app <- Ambiorix$new()
app$get("/", function(req, res){
res$send("Using {ambiorix}!")
})
app$on_stop <- function(){
cat("Bye!\n")
}
if(interactive())
app$start()
## ------------------------------------------------
## Method `Ambiorix$listen`
## ------------------------------------------------
app <- Ambiorix$new()
app$listen(3000L)
app$get("/", function(req, res){
res$send("Using {ambiorix}!")
})
if(interactive())
app$start()
## ------------------------------------------------
## Method `Ambiorix$set_404`
## ------------------------------------------------
app <- Ambiorix$new()
app$set_404(function(req, res){
res$send("Nothing found here")
})
app$get("/", function(req, res){
res$send("Using {ambiorix}!")
})
if(interactive())
app$start()
## ------------------------------------------------
## Method `Ambiorix$set_error`
## ------------------------------------------------
# my custom error handler:
error_handler <- function(req, res, error) {
if (!is.null(error)) {
error_msg <- conditionMessage(error)
cli::cli_alert_danger("Error: {error_msg}")
}
response <- list(
code = 500L,
msg = "Uhhmmm... Looks like there's an error from our side :("
)
res$
set_status(500L)$
json(response)
}
# handler for GET at /whoami:
whoami <- function(req, res) {
# simulate error (object 'Pikachu' is not defined)
print(Pikachu)
}
app <- Ambiorix$
new()$
set_error(error_handler)$
get("/whoami", whoami)
if (interactive()) {
app$start(open = FALSE)
}
## ------------------------------------------------
## Method `Ambiorix$start`
## ------------------------------------------------
app <- Ambiorix$new()
app$get("/", function(req, res){
res$send("Using {ambiorix}!")
})
if(interactive())
app$start(port = 3000L)
## ------------------------------------------------
## Method `Ambiorix$serialiser`
## ------------------------------------------------
app <- Ambiorix$new()
app$serialiser(function(data, ...){
jsonlite::toJSON(x, ..., pretty = TRUE)
})
app$get("/", function(req, res){
res$send("Using {ambiorix}!")
})
if(interactive())
app$start()
Run the code above in your browser using DataLab