通过带有查询参数的重写URL访问Couch DB数据库URL [英] Access Couch DB database URL through rewritten URL, with query parameters
问题描述
我的网站用完了Couch数据库实例,因此我的虚拟主机配置为指向 / dbname / _design / app / _rewrite
。
I have my web site run out of a Couch DB instance, so I have my vhost configured to point to /dbname/_design/app/_rewrite
.
我希望能够通过Web浏览器访问索引页面,同时仍通过Ajax访问Couch DB API,因此我在<$中设置了一对重写规则。 c $ c>重写字段:
I want to be able to access the index page from a web browser, while still accessing the Couch DB API over Ajax, so I set up a pair of rewrite rules in my rewrites
field:
[ { "from": "/dbname/*", "to: ../../*" },
{ "from": "/*", "to: *" } ]
这些规则很好用:我可以通过 / dbname / docname
URL访问单个文档,并且可以指向网站根目录中的Web浏览器,并以这种方式访问我的附件。
These rules work fine: I can access individual documents through a /dbname/docname
URL, and I can point my web browser at the root of the site and access my attachments that way.
我现在想访问数据库本身的信息,以便传递自
参数以来的 _changes
API。
I'd now like to access the information on the database itself, in order to pass a since
parameter to the _changes
API.
-
/ dbname /
可以正常工作 -
/ dbname /?name = value
不能正确重定向。在Couch DB日志中,我看到类似'GET'/dbname/_design/..?name=value 404
的行,而我希望看到的是'GET'/ dbname /?name = value 200
。
/dbname/
works fine/dbname/?name=value
doesn't redirect properly. In the Couch DB log, I see lines like'GET' /dbname/_design/..?name=value 404
, whereas I'd expect to see'GET' /dbname/?name=value 200
.
第二种情况是IE中的Ajax,其中 jquery.couch.js
代码添加了一个伪造的查询字符串以避免缓存。
The second case is needed for Ajax from IE, where the jquery.couch.js
code adds a fake query string to avoid caching.
如何我可以表达我的重写规则,以便Couch DB正确重写 / dbname /?name = value
吗?
How can I phrase my rewrite rules so that Couch DB rewrites /dbname/?name=value
correctly?
编辑:为明确起见,只要URL的最后一个/后面有内容,查询字符串就可以正常工作。
To clarify, query strings work OK as long as there is something after the last / in the URL.
-
/ dbname / docname?rev = xxx
有效 -
/ dbname / _changes?since = 1
有效 -
/ dbname /?_ = dummy
不起作用;它会重写为/ dbname / _design /..__= dummy
/dbname/docname?rev=xxx
works/dbname/_changes?since=1
works/dbname/?_=dummy
doesn't work; it rewrites to/dbname/_design/..?_=dummy
推荐答案
我试图重复您的问题,但是它可以正常工作。以下是我的互动。 (请注意,我使用IP地址 127.0.0.1:5984
来确保没有虚拟主机/重写问题,然后通过 localhost:5984
。
I tried to duplicate your problem but it is working. Below is my interaction. (Note, I use the IP address, 127.0.0.1:5984
, to ensure no vhost/rewrite problems, then I access the "production" site via localhost:5984
.
存在一个错误,它似乎将查询参数附加到以 ..。而不是重写为 ../?key = val
,而是写为 ..?key = val
There is a bug it seems with query parameters being appended to rewrites ending with "..". Instead of rewriting to ../?key=val
it writes to ..?key=val
which CouchDB does not parse.
我认为没有必要使用参数查询数据库URL。因此一种解决方法是始终确保您永远不要这样做。例如,如果您盲目地将no-op参数附加到所有查询以简化代码,则必须进行更改。)
I do not think it is necessary to query a database URL with parameters. So one workaround is to always make sure you never do that. (E.g. if you blindly append no-op parameters to all queries to simplify the code, you'd have to alter that.)
另一种解决方法是启用对 root CouchDB URL 。这需要将 / _ config / httpd / secure_rewrites
设置为 false
。 p>
Another workaround is to enable rewrite to the root CouchDB URL. This requires setting /_config/httpd/secure_rewrites
to false
.
{ "from":"/api/*", "to":"../../../*" }
现在您可以查询 http:// localhost:5984 / api / ? y = val
或 http:// localhost:5984 / api / x / _changes?since = 5
。 (您无法使用参数查询根URL,这仍然是bug,但在人流较少的地方。)
Now you can query http://localhost:5984/api/x?key=val
or http://localhost:5984/api/x/_changes?since=5
. (You cannot query the root URL with parameters—it's still the bug, but in a less trafficked place.)
以下是初始终端会话:
$ mkdir t
$ cd t
$ curl -XDELETE 127.0.0.1:5984/x
{"ok":true}
$ curl -XPUT 127.0.0.1:5984/x
{"ok":true}
$ curl 127.0.0.1:5984
{"couchdb":"Welcome","version":"1.0.1"}
$ echo -n _design/test > _id
$ mkdir shows
$ echo 'function() { return "hello world!\n" }' > shows/hello.js
$ cat > rewrites.json
[ { "from":"/db/*", "to":"../../*" }
, { "from":"/*" , "to":"*"}
]
$ echo '{}' > .couchapprc
$ couchapp push http://127.0.0.1:5984/x
$ curl -XPUT http://127.0.0.1:5984/_config/vhosts/localhost:5984 -d '"/x/_design/test/_rewrite"'
"/x/_design/test/_rewrite"
$ curl localhost:5984 # This is the design document.
{"_id":"_design/test","_rev":"1-e523efd669aa5375e711f8e4b764da7a","shows":{"hello":"function() { return \"hello world!\\n\" }"},"couchapp":{"signatures":{},"objects":{},"manifest":["rewrites.json","shows/","shows/hello.js"]},"rewrites":[{"to":"../../*","from":"/db/*"},{"to":"*","from":"/*"}]}
$ curl localhost:5984/_show/hello
hello world!
$ curl localhost:5984/db # This is the DB.
{"db_name":"x","doc_count":1,"doc_del_count":0,"update_seq":1,"purge_seq":0,"compact_running":false,"disk_size":4185,"instance_start_time":"1298269455135987","disk_format_version":5,"committed_update_seq":1}
$ curl localhost:5984/db/_changes
{"results":[
{"seq":1,"id":"_design/test","changes":[{"rev":"1-e523efd669aa5375e711f8e4b764da7a"}]}
],
"last_seq":1}
$ curl localhost:5984/db/_changes?since=1 # Parameters accepted!
{"results":[
],
"last_seq":1}
这篇关于通过带有查询参数的重写URL访问Couch DB数据库URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!