serveStaticFile method

Future<void> serveStaticFile(
  1. HttpRequest request,
  2. String filePath
)

Serves a static file at filePath to the client.

The file is sent to the client with the correct MIME type.

If the file does not exist, a 404 error is sent to the client and the request is not closed.

Implementation

Future<void> serveStaticFile(HttpRequest request, String filePath) async {
  final file = File(filePath);

  if (await file.exists()) {
    final fileStream = file.openRead();
    await fileStream.pipe(request.response);
  } else {
    _handle404(request);
  }
}