Skip to content

Update shelf_router documentation with middleware example #416

Closed
@andyhorn

Description

@andyhorn

Please update the shelf_router documentation to include an example that uses middleware.

I was unaware that I had to create my Router, set up my routes, and then pass it into a Pipeline. The only shelf_router examples I could find only ever show how to set up the Router and then pass it to the serve function. It took several days before I finally found a StackOverflow question that happened to show how to properly set up the pipeline.

As someone new to shelf and shelf_router, this wasn't immediately obvious.

Something like the following block would have been immensely helpful:

import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart' as io;

void main() async {
  // set up your router and routes
  final router = Router()..get('/', () => Response.ok('hello world!'));

  // set your router as the handler for your pipeline
  final app = const Pipeline().addMiddleware(logRequests()).addHandler(router.call);

  // pass your pipeline into the serve method
  await io.serve(app.call, '0.0.0.0', 3000);
}

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions