Class URLRouter
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.
The following example shows how these are used.
Registered routes are matched in the same sequence as initially specified. 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 may generate a 404 error.
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"]); } static this() { 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); // Natches 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); }
Inherits from
-
(base class)Object
-
HTTPRouter
Methods
Name | Description |
---|---|
getAllRoutes
|
Returns all registered routes as const AA |
handleRequest
|
Handles a HTTP request by dispatching it to the registered route handlers. |
match
|
Adds a new route for requests matching the specified HTTP method and pattern.
|
any
|
Adds a new route for requests matching the specified pattern, regardless of their HTTP verb. |
delete_
|
Adds a new route for DELETE requests matching the specified pattern. |
get
|
Adds a new route for GET requests matching the specified pattern. |
match
|
Adds a new route for request that match the path and method
|
patch
|
Adds a new route for PATCH requests matching the specified pattern. |
post
|
Adds a new route for POST requests matching the specified pattern. |
put
|
Adds a new route for PUT requests matching the specified pattern. |
Aliases
Name | Description |
---|---|
match
|
Authors
Sönke Ludwig
Copyright
© 2012 RejectedSoftware e.K.
License
Subject to the terms of the MIT license, as written in the included LICENSE.txt file.