Skip to content

Route handler

Ravi Teja Gudapati edited this page Jan 9, 2019 · 25 revisions

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.

What's next?

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

Serialization

Forms

Sessions

Authentication

  • Basic authentication
  • Form authentication
  • JSON authentication
  • Authorization
  • OAuth

Database

Security

Real time

  • Server sent events (SSE)
  • Websockets

Deployment

  • systemd
  • Docker
  • AppEngine

API Documentation

Clone this wiki locally