Learn R Programming

servr (version 0.32)

create_server: Create a server

Description

Create a server with a custom handler to handle the HTTP request.

Usage

create_server(..., handler, ws_open = function(ws) NULL)

Arguments

...

Arguments to be passed to server_config().

handler

A function that takes the HTTP request and returns a response.

ws_open

A function to be called back when a WebSocket connection is established (see httpuv::startServer()).

Examples

Run this code
if (FALSE) { # interactive()
# always return 'Success:' followed by the requested path
s = servr::create_server(handler = function(req) {
    list(status = 200L, body = paste("Success:", req$PATH_INFO))
})
s$url

browseURL(paste0(s$url, "/hello"))
browseURL(paste0(s$url, "/world"))

s$stop_server()
}

Run the code above in your browser using DataLab