-
Notifications
You must be signed in to change notification settings - Fork 35
Route handler
A RouteHandler is a function, method or closure that processes an incoming HTTP Request
and returns the Response
. The prototype of route handler is:
typedef FutureOr<dynamic> RouteHandler(Context ctx);
The Context
encapsulates the entire context of a HTTP request. Among other things, it exposes the HTTP Request
as member req
.
Here is an example of adding a simple route handler function to Jaguar server at HTTP path /hello
:
import 'package:jaguar/jaguar.dart';
main() async {
final server = Jaguar(); // Serves the API at localhost:8080 by default
// Add a route handler for 'GET' method at path '/hello'
server.get('/hello', (Context ctx) => 'Hello world!');
await server.serve();
}
Note that the return type of the route handler is dynamic
and not the expected Response
. This is because, in cases where the return value is not Response
, Jaguar will automatically build a Response
object from the returned value.
In this article, we saw how to invoke a route handler when the request uri exactly matches /hello
.
In the next article, Path matching, we will learn to take advantage of Jaguar's powerful path matching techniques to invoke route handlers based on regular expressions and globs.
Basics
- Route handler
- Path matching
- Path parameters
- Query parameters
- Serving static files
- Cookies
- Controller
- Parameter binding
- Hot reload
Serialization
Forms
Sessions
Authentication
- Basic authentication
- Form authentication
- JSON authentication
- Authorization
- OAuth
- MongoDb
- PostgreSQL
- MySQL
- Establish connection
- ORM
- Server sent events (SSE)
- Websockets
- systemd
- Docker
- AppEngine