as.route: Coercing Objects to Routes

Description Usage Arguments Details Value See Also Examples

Description

The function as.route provides an alternative means of creating an application route.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
as.route(x, ...)

## S3 method for class 'route'
as.route(x, ...)

## S3 method for class 'character'
as.route(x, directory = "routes", ...)

## S3 method for class 'list'
as.route(x, ...)

Arguments

x

An R object.

directory

System path to the folder containing the route file.

...

Additional arguments passed on to methods.

Details

If x is a list, x must have the following named elements: method, path, and handler.

If x is a character vector, x is interpreted as a file name. The file must contain a route defined using the route function. The default directory for route files is "routes", but a different folder may be specified with the directory argument.

The S3 generic function as.route is exported by prairie to encourage creation of as.route.* functions. Custom as.route functions allow users to coerce their classes to routes and quickly serve them over HTTP.

Value

A route object.

See Also

route

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Easily reuse routes and keep applications
# modular by storing routes in separate files.

tmp <- tempfile()
writeLines(
  'route("GET", "^$", function(req) response())\n',
  con = tmp
)

as.route(tmp, dir = '')

file.remove(tmp)

# as.route.list is a minimal wrapper
# around route()

route(
  'POST',
  '^$',
  function(req) {
    response()
  }
)

as.route(
  list(
    method = 'POST',
    path = '^$',
    handler = function(req) {
      response()
    }
  )
)

nteetor/prairie documentation built on May 24, 2019, 9:56 a.m.