serveRequests function

void serveRequests(
  1. Stream<HttpRequest> requests,
  2. Handler handler, {
  3. String? poweredByHeader = 'Dart with package:shelf',
})

Serve a Stream of HttpRequests.

HttpServer implements Stream<HttpRequest> so it can be passed directly to serveRequests.

Errors thrown by handler while serving a request will be printed to the console and cause a 500 response with no body. Errors thrown asynchronously by handler will be printed to the console or, if there's an active error zone, passed to that zone.

Every response will get a "date" header and an "X-Powered-By" header. If the either header is present in the Response, it will not be overwritten. Pass poweredByHeader to set the default content for "X-Powered-By", pass null to omit this header.

Implementation

void serveRequests(
  Stream<HttpRequest> requests,
  Handler handler, {
  String? poweredByHeader = 'Dart with package:shelf',
}) {
  catchTopLevelErrors(() {
    requests.listen((request) =>
        handleRequest(request, handler, poweredByHeader: poweredByHeader));
  }, (error, stackTrace) {
    _logTopLevelError('Asynchronous error\n$error', stackTrace);
  });
}