1.5.13. /db/_design/design-doc/_rewrite/path

    Rewrites the specified path by rules defined in the specified design document. The rewrite rules are defined by the rewrites field of the design document. The rewrites field can either be a string containing the a rewrite function or an array of rule definitions.

    New in version 2.0: When the rewrites field is a stringified function, the query server is used to pre-process and route requests.

    The function takes a Request2 object.

    The return value of the function will cause the server to rewrite the request to a new location or immediately return a response.

    • path (string): Rewritten path.
    • query (array): Rewritten query. If omitted, the original query keys are used.
    • headers (object): Rewritten headers. If omitted, the original request headers are used.
    • method (string): HTTP method of rewritten request ("GET", "POST", etc). If omitted, the original request method is used.
    • body (string): Body for "POST"/"PUT" requests. If omitted, the original request body is used.

    To immediately respond to the request, return an object containing the following properties:

    • code (number): Returned HTTP status code (200, 404, etc).
    • body (string): Body of the response to user.

    Example A. Restricting access.

    Example B. Different replies for JSON and HTML requests.

    1. var path = req2.path.slice(4),
    2. h = headers,
    3. wantsJson = (h.Accept || "").indexOf("application/json") > -1,
    4. reply = {};
    5. if (!wantsJson) {
    6. // for plain HTML pages
    7. } else {
    8. // Pass through JSON requests
    9. }
    10. return reply;
    11. }

    1.5.13.2. Using an array of rules for rewrites