Skip to content

Commit 1acbc67

Browse files
authored
Add shelf_router middleware examples (#417)
Update the `shelf_router` README with an example of how to configure middleware Fixes #416
1 parent da6a69b commit 1acbc67

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

pkgs/shelf_router/README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,23 @@ import 'package:shelf_router/shelf_router.dart';
1818
import 'package:shelf/shelf.dart';
1919
import 'package:shelf/shelf_io.dart' as io;
2020
21-
var app = Router();
21+
// instantiate a router and configure your routes
22+
var router = Router();
2223
23-
app.get('/hello', (Request request) {
24+
router.get('/hello', (Request request) {
2425
return Response.ok('hello-world');
2526
});
2627
27-
app.get('/user/<user>', (Request request, String user) {
28+
router.get('/user/<user>', (Request request, String user) {
2829
return Response.ok('hello $user');
2930
});
3031
32+
// use a Pipeline to configure your middleware,
33+
// then add the router as the handler
34+
final app = const Pipeline()
35+
.addMiddleware(logRequests())
36+
.addHandler(router);
37+
3138
var server = await io.serve(app, 'localhost', 8080);
3239
```
3340

pkgs/shelf_router/example/main.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ class Service {
5353
return Response.notFound('Page not found');
5454
});
5555

56-
return router.call;
56+
// Set up your Pipeline with any middleware you want to use and set the
57+
// router as the handler.
58+
return const Pipeline()
59+
.addMiddleware(logRequests())
60+
.addHandler(router.call);
5761
}
5862
}
5963

0 commit comments

Comments
 (0)