Routes
A route binds an HTTP method + URL pattern to a handler. Garvan inherits Crow's compile-time router, which builds a decision tree from your route definitions — lookups are O(log n) and there's no regex involved.
The CROW_ROUTE macro
The canonical way to declare a route:
CROW_ROUTE(app, "/hello")([] {
return "hi";
});
You can also use app.route_dynamic("/hello") if you need to construct paths at runtime, but the macro form is preferred because it's checked at compile time.
URL parameters
Place parameters in angle brackets and accept them as arguments to your handler. Supported types are int, uint, double, string and path:
CROW_ROUTE(app, "/users/<int>")([](int id) {
return std::to_string(id);
});
CROW_ROUTE(app, "/files/<path>")([](std::string rest) {
return "you requested: " + rest;
});
HTTP methods
Routes default to GET. Use .methods(...) to register additional verbs:
CROW_ROUTE(app, "/users")
.methods("GET"_method, "POST"_method)
([](const crow::request &req) {
if (req.method == crow::HTTPMethod::POST) {
return crow::response(201, "created");
}
return crow::response("user list");
});
Handler
A handler can take zero or more arguments. The first argument may be const crow::request& to access headers and the body; the second may be crow::response& if you want to write the response incrementally and call res.end() yourself.
Response
Return a std::string for the body, or a crow::response when you need to set the status code or headers explicitly:
return crow::response(404, "not found");
Catchall routes
Use CROW_CATCHALL_ROUTE(app) to define a fallback that runs whenever no other route matches. The docs site you're reading right now uses one to resolve unknown paths against a page index.
CROW_CATCHALL_ROUTE(app)([](const crow::request &req) {
return crow::response(404, "no such page: " + req.url);
});