vibe.d beta banner
get vibe.d
0.8.1

Asynchronous I/O that doesn’t get in your way, written in D

Class URLRouter

Routes HTTP requests based on the request method and URL.

class URLRouter
  : HTTPRouter ;

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 will generate a 404 error.

Constructors

NameDescription
this

Properties

NameTypeDescription
prefix[get]string

Methods

NameDescription
getAllRoutesReturns all registered routes as const AA
handleRequestHandles a HTTP request by dispatching it to the registered route handlers.
matchAdds a new route for requests matching the specified HTTP method and pattern.
rebuildRebuilds the internal matching structures to account for newly added routes.
anyAdds 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.
getAdds a new route for GET requests matching the specified pattern.
matchAdds a new route for request that match the path and method
patchAdds a new route for PATCH requests matching the specified pattern.
postAdds a new route for POST requests matching the specified pattern.
putAdds a new route for PUT requests matching the specified pattern.

Aliases

NameDescription
match

Example

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);

	// 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);
}

Example

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/:user -> showComponentUser

	// Start the HTTP server
	auto settings = new HTTPServerSettings;
	// ...
	listenHTTP(settings, mainrouter);
}
Authors

Sönke Ludwig

Copyright

© 2012-2014 RejectedSoftware e.K.

License

Subject to the terms of the MIT license, as written in the included LICENSE.txt file.