dart-lang / shelf

Web server middleware for Dart
https://pub.dev/packages/shelf
BSD 3-Clause "New" or "Revised" License
921 stars 124 forks source link

Different response for 404 #412

Closed leandrogaldino closed 7 months ago

leandrogaldino commented 7 months ago

In the code below I created two separate APIs, ApiA and ApiB, each with only one endpoint returning a 404.

Why I only receive the "Response.notFound('body test', headers: {'head-test': 'test'})" in the last API added to the Cascade in the Main? In this example, ApiA is returning a "Response.notFound('Route not found')" without additional headers.

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

void main() async {
  var apiAHandler = ApiA();
  var apiBHandler = ApiB();
  var cascadeHandler = Cascade().add(apiAHandler.router).add(apiBHandler.router).handler;
  await shelf_io.serve(cascadeHandler, 'localhost', 8080);
  print('Server Online');
}

class ApiA {
  Router get router {
    Router router = Router();
    router.get('/apia', (Request req) async {
      return Response.notFound('body test', headers: {'head-test': 'test'});
    });
    return router;
  }
}

class ApiB {
  Router get router {
    Router router = Router();
    router.get('/apib', (Request req) async {
      return Response.notFound('body test', headers: {'head-test': 'test'});
    });
    return router;
  }
}
leandrogaldino commented 7 months ago

Instead of using a cascade, I nested the APIs with router.mount and voilà!