Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support wildcard/fallback routes #885

Open
enriquecaballero opened this issue Nov 18, 2022 · 1 comment
Open

Support wildcard/fallback routes #885

enriquecaballero opened this issue Nov 18, 2022 · 1 comment

Comments

@enriquecaballero
Copy link

enriquecaballero commented Nov 18, 2022

I couldn't seem to find a way to implement this and it doesn't seem like there are any examples online: at the moment frontend routers such as React Router aren't compatible with Plumber as there doesn't seem to be a way to create a wildcard/fallback route they require. Usually with other api building frameworks, such as Node.js Express, I usually have a route at the end of my other routes as such:

app.use("/", (req, res) => {
  res.sendFile(path.resolve("./static", "index.html"));
});

Would be essential to support a fallback/wildcard route to ensure frontend router compatibility.

...

(If this is already implemented or can be implemented programmatically, could you please provide an example?)

@schloerke
Copy link
Collaborator

This solution would ruin API docs until #882 or #883 is implemented.


Very clunky, but there is a work around. (See static file handler for an example.)

You can mount a router at /. This mount should have a filter attached to it to handle all requests that it receives.

## Untested

#' @plumber
function(root) {
  fallback <- plumber::pr()
  fallback$filter("fallback", function(req, res) {
    # Do not call `plumber::forward()`
    # Return fallback info
    res.sendFile(path.resolve("./static", "index.html"));
  })

  root$mount("/", fallback)
}

This should also work if you want to perform this on a sub route.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants