Build Web APIs with Aqueduct #2 (video series)
Jermaine

Jermaine @creativ_bracket

About: Content Creator | Dart/Flutter Developer

Location:
London, UK
Joined:
Apr 21, 2018

Build Web APIs with Aqueduct #2 (video series)

Publish Date: Dec 4 '18
9 0

In this article we will look at routing in further detail, building an endpoint capable of performing CRUD(create, read, update, delete) operations. We will make use of Router and Controller classes in the handling of requests made to our endpoint.

Here's the second video in the series:


Watch on YouTube


What is a Router?

Routers capture the request by path and forwards it to a Controller using a link method for resolution or a linkFunction to implement middleware functionality to handle the request.

Here's what it looks like:

final router = Router();

router.route('/path-1').link(() => Path1Controller());
router.route('/path-2').linkFunction((Request req) async {
  return Response.ok('Hello, World!');
});
Enter fullscreen mode Exit fullscreen mode

The path is passed as an argument when route() is invoked, and can also accept route parameters:

router.route('/path-1/:name').link(...);
router.route('/path-2/[:name]').linkFunction(...); // wrapping in square brackets means its optional
Enter fullscreen mode Exit fullscreen mode

So with this snippet whatever is passed in the path segment after /path-1/ is captured in name path variable:

router.route('/path-1/:name').linkFunction((request) async {
  final name = request.path.variables['name'];
  return Response.ok('Hello, $name');
});
Enter fullscreen mode Exit fullscreen mode

See how routers and controllers work in this video.

Get the source code


Subscribe to my YouTube channel for more videos covering various aspects of full-stack web development with Dart.

Like, share and follow me 😍 for more content on Dart.

Further reading

Comments 0 total

    Add comment