rewriteTo function

Middleware rewriteTo (String absolutePath, bool shouldBeRewritten(Request request, Response response))

Creates a Middleware that will replace the path of requestedUri with absolutePath if the request shouldBeRewritten.

This needs some understanding of package: shelf.

Implementation

Middleware rewriteTo(String absolutePath,
        bool shouldBeRewritten(Request request, Response response)) =>
    (innerHandler) => (request) async {
          var requestBody = await request.readAsString(request.encoding);
          var response = await innerHandler(request.change(body: requestBody));
          return shouldBeRewritten(request, response)
              ? await innerHandler(new Request(request.method,
                  request.requestedUri.replace(path: absolutePath),
                  body: requestBody,
                  context: request.context,
                  encoding: request.encoding,
                  headers: request.headers,
                  protocolVersion: request.protocolVersion))
              : response;
        };