-
Notifications
You must be signed in to change notification settings - Fork 35
Route handler
A Route handler is a function or method that processes an incoming HTTP Request
and returns the Response
. The prototype of route handler is:
typedef FutureOr<dynamic> RouteFunc(Context ctx);
The Context
encapsulates the entire context of a HTTP request. Among other things, it contains the Request
object of the current HTTP request as member req
.
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.
Here is an example of adding a simple route handler function to Jaguar server at route path /hello
:
import 'package:jaguar/jaguar.dart';
main() async {
final server = new 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();
}
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