cors {beakr} | R Documentation |
Allow Cross-Origin-Requests
Description
Allow Cross-Origin Resource Sharing headers as described in MDN Web Docs. Cross-origin resource sharing is a mechanism that allows restricted resources on a web page to be requested from another domain(origin) outside the domain from which the first resource was served.
Usage
cors(
beakr,
path = NULL,
methods = c("GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"),
origin = "*",
credentials = NULL,
headers = NULL,
maxAge = NULL,
expose = NULL
)
Arguments
beakr |
|
path |
String representing a path for which to specify a CORS policy.
Default |
methods |
A vector of the request methods to allow. i.e
|
origin |
A vector of the request origin(s) for which resource sharing
is enabled. i.e |
credentials |
A boolean to enable/disable credentialed requests. i.e
|
headers |
A vector of the allowed headers. i.e
|
maxAge |
The max age, in seconds. i.e |
expose |
The headers to expose. i.e |
Value
A Beakr
instance with CORS enabled
Note
You can verify that CORS is enabled by using the Chrome browser and
opening up the Developer Tools. The "Network" tab allows you to inspect
response headers and see where the Cross-Origin
policy is specified.
If you run the example in the console, be sure to
stopServer(bekar)
when you are done.
See Also
Examples
library(beakr)
# Create an new beakr instance
beakr <- newBeakr()
# beakr pipeline
beakr %>%
# Enable CORS
cors() %>%
# Respond to GET requests at the "/hi" route
httpGET(path = "/hi", function(req, res, err) {
print("Hello, World!")
}) %>%
# Respond to GET requests at the "/bye" route
httpGET(path = "/bye", function(req, res, err) {
print("Farewell, my friends.")
}) %>%
# Start the server on port 25118
listen(host = "127.0.0.1", port = 25118, daemon = TRUE)
# ------------------------------------------------------------
# POINT YOUR BROWSER AT:
# * http://127.0.0.1:25118/hi
# * http://127.0.0.1:25118/bye
#
# THEN, STOP THE SERVER WITH stopServer(beakr)
# ------------------------------------------------------------
# Stop the beakr instance server
stopServer(beakr)