Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update shelf_router documentation with middleware example #416

Closed
andyhorn opened this issue Mar 5, 2024 · 1 comment
Closed

Update shelf_router documentation with middleware example #416

andyhorn opened this issue Mar 5, 2024 · 1 comment

Comments

@andyhorn
Copy link
Contributor

andyhorn commented Mar 5, 2024

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!

@kevmoo
Copy link
Member

kevmoo commented Mar 5, 2024

PRs welcome!

@kevmoo kevmoo closed this as completed in 1acbc67 Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants