rewriteTo function

Middleware rewriteTo (String absolutePath, [ bool shouldBeRewritten(Request request) = _hasNoMimeType ])

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

By default, the request that shouldBeRewritten is the one whose url doesn't have a MIME type. If this heuristic doesn't work well for you, please file an issue. Furthermore, PRs are welcome.

This needs some understanding of package: shelf.

Implementation

Middleware rewriteTo(String absolutePath,
        [bool shouldBeRewritten(Request request) = _hasNoMimeType]) =>
    (innerHandler) => (request) => shouldBeRewritten(request)
        ? innerHandler(new Request(
            request.method, request.requestedUri.replace(path: absolutePath),
            body: request.read(),
            context: request.context,
            encoding: request.encoding,
            headers: request.headers,
            protocolVersion: request.protocolVersion))
        : innerHandler(request);