4.2. 使用HTML表单提交文档

通过使用文档,可以直接从HTML表单写入CouchDB文档 update function . 方法如下:

4.2.1. HTML表单

首先,编写一个HTML表单。下面是一个简单的“联系我们”表格摘录:

<form action="/dbname/_design/ddocname/_update/contactform" method="post">
    <div>
        <label for="name">Name:</label>
        <input type="text" id="name" name="name" />
    </div>
    <div>
        <label for="mail">Email:</label>
        <input type="text" id="mail" name="email" />
    </div>
    <div>
        <label for="msg">Message:</label>
        <textarea id="msg" name="message"></textarea>
    </div>
</form>

自定义 /dbname/_design/ddocname/_update/contactform 表单操作URL的一部分,用于反映数据库、设计文档和更新函数的确切路径(请参见下文)。

作为CouchDB no longer recommends the use of CouchDB-hosted web applications ,您可能希望使用反向代理将CouchDB公开为web应用程序的子目录。如果是,请将该前缀添加到 action 表单中的目标。

另一个选择是修改CouchDB的 CORS 设置并使用跨域帖子。 在执行此操作之前,请确保您了解所有安全问题!

4.2.2. 更新功能

然后,编写一个更新函数。这是服务器端JavaScript函数,它将接收 POST -ed数据。

函数的第一个参数将是正在处理的文档(如果存在)。因为我们正在使用 POST 而不是 PUT ,这在我们的场景中应该是空的-但是我们应该检查以确定。这个 POST -ed数据将作为第二个参数传递给函数,以及任何查询参数和完整的请求头。

下面是一个示例处理程序,它提取表单数据,根据电子邮件地址和时间戳生成文档id,并保存文档。然后将JSON成功响应返回给浏览器。

function(doc, req) {

    if (doc) {
        return [doc, toJSON({"error": "request already filed"})]
    }

    if !(req.form && req.form.email) {
        return [null, toJSON({"error": "incomplete form"})]
    }

    var date = new Date()
    var newdoc = req.form
    newdoc._id = req.form.email + "_" + date.toISOString()

    return [newdoc, toJSON({"success":"ok"})]
}

将上述功能放在设计文档中的 updates 关键。

请注意,此函数不尝试任何类型的输入验证或卫生处理。最好由 validate document update function 相反。(“VDU”将验证写入数据库的任何文档,而不仅仅是那些使用更新功能的文档。)

如果第一个元素传递给 return 是一个文档,则HTTP响应头将包括 X-Couch-Id , the _id 新创建文档的值,以及 X-Couch-Update-NewRev , the _rev 新创建文档的值。如果您的客户端代码希望在将来的调用中访问或更新文档,这将非常方便。

4.2.3. 实例输出

这是上面的工作样本,使用 curl 模拟模板柱。

$ curl -X PUT localhost:5984/testdb/_design/myddoc -d '{ "updates": { "contactform": "function(doc, req) { ... }" } }'
{"ok":true,"id":"_design/myddoc","rev":"1-2a2b0951fcaf7287817573b03bba02ed"}

$ curl --data "name=Lin&email=lin@example.com&message=I Love CouchDB" http://localhost:5984/testdb/_design/myddoc/_update/contactform
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 5984 (#1)
> POST /testdb/_design/myddoc/_update/contactform HTTP/1.1
> Host: localhost:5984
> User-Agent: curl/7.59.0
> Accept: */*
> Content-Length: 53
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 53 out of 53 bytes
< HTTP/1.1 201 Created
< Content-Length: 16
< Content-Type: text/html; charset=utf-8
< Date: Thu, 05 Apr 2018 19:56:42 GMT
< Server: CouchDB/2.2.0-948a1311c (Erlang OTP/19)
< X-Couch-Id: lin%40example.com_2018-04-05T19:51:22.278Z
< X-Couch-Request-ID: 03a5f4fbe0
< X-Couch-Update-NewRev: 1-34483732407fcc6cfc5b60ace48b9da9
< X-CouchDB-Body-Time: 0
<
* Connection #1 to host localhost left intact
{"success":"ok"}

$ curl http://localhost:5984/testdb/lin\@example.com_2018-04-05T19:51:22.278Z
{"_id":"lin@example.com_2018-04-05T19:51:22.278Z","_rev":"1-34483732407fcc6cfc5b60ace48b9da9","name":"Lin","email":"lin@example.com","message":"I Love CouchDB"}