Adds a new route for requests matching the specified pattern, regardless of their HTTP verb.
Adds a new route for DELETE requests matching the specified pattern.
Adds a new route for GET requests matching the specified pattern.
Returns all registered routes as const AA
Handles a HTTP request by dispatching it to the registered route handlers.
Adds a new route for requests matching the specified HTTP method and pattern.
Adds a new route for PATCH requests matching the specified pattern.
Adds a new route for POST requests matching the specified pattern.
Adds a new route for PUT requests matching the specified pattern.
Rebuilds the internal matching structures to account for newly added routes.
Returns a single route handle to conveniently register multiple methods.
Controls the computation of the "routerRootDir" parameter.
Sets a common prefix for all registered routes.
Match patterns are character sequences that can optionally contain placeholders or raw wildcards ("*"). Raw wild cards match any character sequence, while placeholders match only sequences containing no slash ("/") characters.
Placeholders are started using a colon (":") and are directly followed by their name. The first "/" character (or the end of the match string) denotes the end of the placeholder name. The part of the string that matches a placeholder will be stored in the HTTPServerRequest.params map using the placeholder name as the key.
Match strings are subject to the following rules:
import vibe.http.fileserver; void addGroup(HTTPServerRequest req, HTTPServerResponse res) { // Route variables are accessible via the params map logInfo("Getting group %s for user %s.", req.params["groupname"], req.params["username"]); } void deleteUser(HTTPServerRequest req, HTTPServerResponse res) { // ... } void auth(HTTPServerRequest req, HTTPServerResponse res) { // TODO: check req.session to see if a user is logged in and // write an error page or throw an exception instead. } void setup() { auto router = new URLRouter; // Matches all GET requests for /users/*/groups/* and places // the place holders in req.params as 'username' and 'groupname'. router.get("/users/:username/groups/:groupname", &addGroup); // Matches all requests. This can be useful for authorization and // similar tasks. The auth method will only write a response if the // user is _not_ authorized. Otherwise, the router will fall through // and continue with the following routes. router.any("*", &auth); // Matches a POST request router.post("/users/:username/delete", &deleteUser); // Matches all GET requests in /static/ such as /static/img.png or // /static/styles/sty.css router.get("/static/*", serveStaticFiles("public/")); // Setup a HTTP server... auto settings = new HTTPServerSettings; // ... // The router can be directly passed to the listenHTTP function as // the main request handler. listenHTTP(settings, router); }
Using nested routers to map components to different sub paths. A component could for example be an embedded blog engine.
// some embedded component: void showComponentHome(HTTPServerRequest req, HTTPServerResponse res) { // ... } void showComponentUser(HTTPServerRequest req, HTTPServerResponse res) { // ... } void registerComponent(URLRouter router) { router.get("/", &showComponentHome); router.get("/users/:user", &showComponentUser); } // main application: void showHome(HTTPServerRequest req, HTTPServerResponse res) { // ... } void setup() { auto c1router = new URLRouter("/component1"); registerComponent(c1router); auto mainrouter = new URLRouter; mainrouter.get("/", &showHome); // forward all unprocessed requests to the component router mainrouter.any("*", c1router); // now the following routes will be matched: // / -> showHome // /component1/ -> showComponentHome // /component1/users/:user -> showComponentUser // Start the HTTP server auto settings = new HTTPServerSettings; // ... listenHTTP(settings, mainrouter); }
Routes HTTP requests based on the request method and URL.
Routes are matched using a special URL match string that supports two forms of placeholders. See the sections below for more details.
Registered routes are matched according to the same sequence as initially specified using match, get, post etc. Matching ends as soon as a route handler writes a response using res.writeBody() or similar means. If no route matches or if no route handler writes a response, the router will simply not handle the request and the HTTP server will automatically generate a 404 error.