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

警告

重写在couchdb3.0中不推荐使用,并将在couchdb4.0中删除。

ANY /{db}/_design/{ddoc}/_rewrite/{path}

使用指定设计文档中定义的规则重写指定的路径。重写规则由 rewrites 设计文件的字段。这个 rewrites 字段可以是 一串 包含重写函数或 数组 规则定义。

1.5.13.1. 使用stringized函数 rewrites

2.0 新版功能: rewrites 字段是一个字符串化函数,查询服务器用于预处理和路由请求。

函数需要一个 Request2对象 .

函数的返回值将导致服务器将请求重写到新位置或立即返回响应。

若要重写请求,请返回包含以下属性的对象:

  • path一串 ):重写路径。
  • 查询数组 ):已重写查询。如果省略,则使用原始查询键。
  • 报头对象 ):重写的标题。如果省略,则使用原始请求头。
  • 方法一串 ):重写请求的HTTP方法 ("GET""POST" 等等)。如果省略,则使用原始请求方法。
  • body一串 ):主体 "POST"/"PUT" 请求。如果省略,则使用原始请求正文。

若要立即响应请求,请返回包含以下属性的对象:

  • code ):返回HTTP状态代码 (200404 等)。
  • body一串 ):对用户的响应正文。

示例A . 限制访问。

function(req2) {
  var path = req2.path.slice(4),
    isWrite = /^(put|post|delete)$/i.test(req2.method),
    isFinance = req2.userCtx.roles.indexOf("finance") > -1;
  if (path[0] == "finance" && isWrite && !isFinance) {
    // Deny writes to  DB "finance" for users
    // having no "finance" role
    return {
      code: 403,
      body: JSON.stringify({
        error: "forbidden".
        reason: "You are not allowed to modify docs in this DB"
      })
    };
  }
  // Pass through all other requests
  return { path: "../../../" + path.join("/") };
}

示例B . JSON和HTML请求的不同回复。

function(req2) {
  var path = req2.path.slice(4),
    h = headers,
    wantsJson = (h.Accept || "").indexOf("application/json") > -1,
    reply = {};
  if (!wantsJson) {
    // Here we should prepare reply object
    // for plain HTML pages
  } else {
    // Pass through JSON requests
    reply.path = "../../../"+path.join("/");
  }
  return reply;
}

1.5.13.2. 使用规则数组 rewrites

rewrites 字段是规则对象的数组,服务器将根据数组中的第一个匹配规则重写请求。

数组中的每个规则都是 对象 包含以下字段:

  • 方法一串 ):HTTP request方法将请求方法绑定到规则。如果省略,则使用 "*" ,它匹配所有方法。
  • from一串 ):用于与URL进行比较并定义动态变量的模式。
  • to一串 ):将URL重写到的路径。它可以包含变量,具体取决于在模式匹配过程中发现的绑定变量和查询参数(URL参数和来自查询成员的)。
  • 查询对象 ):传递给重写URL的查询参数。它们可能包含动态变量。

这个 to and from paths may contains string patterns with leading : or `` *``字符来定义匹配中的动态变量。

第一条规则 rewrites 与传入请求匹配的数组用于定义重写。为了匹配传入的请求,规则 method 必须匹配请求的HTTP方法和规则的 from 必须使用以下模式匹配逻辑匹配请求的路径。

  • 这个 from 模式和URL首先在 / 获取令牌列表。例如,如果 from 字段是 /somepath/:var/* 网址是 /somepath/a/b/c ,代币是 somepath:var* 对于 from 图案和 somepathabc 对于URL。
  • 每个令牌以 : in the pattern will match the corresponding token in the URL and define a new dynamic variable whose name is the remaining string after the : and value is the token from the URL. In this example, the `` :var``标记将匹配 b 并设置 var = a .
  • 星牌 * 在模式中,将匹配URL中任意数量的令牌,并且必须是模式中的最后一个令牌。它将用剩余的标记定义一个动态变量。在本例中 * 令牌将匹配 bc 令牌和集合 * = b/c .
  • 剩余的标记必须完全匹配才能将模式视为匹配。在这个例子中, somepath 在模式匹配中 somepath 与URL中的所有标记匹配,从而使此规则匹配。

找到规则后,将使用 to and query fields. Dynamic variables are substituted into the : and `` *``这些字段中的变量来生成最终的URL。

如果没有规则匹配,则 404 Not Found 返回响应。

实例:

规则 URL 重写为 代币
{“from”:“/a”,
“to”:“/some”}
/a /一些  
{“发件人”:/a/ * "
“To”:“/Some/*}
/a/b/c公司 /部分/b/c  
{“from”:“/a/b”,
“to”:“/some”}
/a/b?k=v /一些?k=v k=v
{“from”:“/a/b”,
“to”:“/some/:var”}
/a/b公司 /一些/b?变量=b 变量=b
{“from”:“/a/:foo/”,
“to”:“/some/:foo/”}
/a/b/c公司 /一些/b/c?foo=b foo=b
{“from”:“/a/:foo”,
“to”:“/some”,“query”:{“k”:“:foo”}}
/a/b公司 /一些/?k=b和foo=b foo=b
{“from”:“/a”,
“to”:“/some/:foo”}
/a?foo=b /一些/?b&foo=b foo=b

请求方法、头、查询参数、请求负载和响应体依赖于URL将重写到的端点。

参数数据库:数据库名称
参数ddoc:设计文件名称
参数路径:要重写的URL路径