Skip to content

Add shelf_router middleware examples #417

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

Merged
merged 2 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions pkgs/shelf_router/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,23 @@ import 'package:shelf_router/shelf_router.dart';
import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart' as io;

var app = Router();
// instantiate a router and configure your routes
var router = Router();

app.get('/hello', (Request request) {
router.get('/hello', (Request request) {
return Response.ok('hello-world');
});

app.get('/user/<user>', (Request request, String user) {
router.get('/user/<user>', (Request request, String user) {
return Response.ok('hello $user');
});

// use a Pipeline to configure your middleware,
// then add the router as the handler
final app = const Pipeline()
.addMiddleware(logRequests())
.addHandler(router);

var server = await io.serve(app, 'localhost', 8080);
```

Expand Down
6 changes: 5 additions & 1 deletion pkgs/shelf_router/example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ class Service {
return Response.notFound('Page not found');
});

return router.call;
// Set up your Pipeline with any middleware you want to use and set the
// router as the handler.
return const Pipeline()
.addMiddleware(logRequests())
.addHandler(router.call);
}
}

Expand Down