我的网站用完了Couch数据库实例,因此我将vhost配置为指向/dbname/_design/app/_rewrite
.
我希望能够从Web浏览器访问索引页面,同时仍然通过Ajax访问Couch DB API,所以我在我的rewrites
字段中设置了一对重写规则:
[ { "from": "/dbname/*", "to: ../../*" },
{ "from": "/*", "to: *" } ]
Run Code Online (Sandbox Code Playgroud)
这些规则运行正常:我可以通过/dbname/docname
URL 访问单个文档,我可以将我的Web浏览器指向站点的根目录并以这种方式访问我的附件.
我现在想要访问数据库本身的信息,以便将since
参数传递给_changes
API.
/dbname/
工作良好/dbname/?name=value
没有正确重定向.在Couch DB日志中,我看到了类似的行'GET' /dbname/_design/..?name=value 404
,而我希望看到'GET' /dbname/?name=value 200
.第二种情况是来自IE的Ajax,其中jquery.couch.js
代码添加了一个伪查询字符串以避免缓存.
如何判断我的重写规则以便Couch DB /dbname/?name=value
正确重写?
编辑:为了澄清,只要在URL中的最后一个/之后存在某些内容,查询字符串就可以正常工作.
/dbname/docname?rev=xxx
作品/dbname/_changes?since=1
作品/dbname/?_=dummy
不起作用; 它重写为/dbname/_design/..?_=dummy
Jas*_*ith 11
我试图复制你的问题,但它正在运作.以下是我的互动.(注意,我使用IP地址,127.0.0.1:5984
以确保没有vhost /重写问题,然后我通过"生产"网站访问localhost:5984
.
将查询参数附加到以".."结尾的重写时,似乎存在一个错误.而不是重写../?key=val
它写入..?key=val
CouchDB不解析.
我认为没有必要使用参数查询数据库URL.因此,一种解决方法是始终确保您永远不会这样做.(例如,如果您盲目地将no-op参数附加到所有查询以简化代码,则必须更改它.)
另一种解决方法是启用对CouchDB根URL的重写.这需要设置/_config/httpd/secure_rewrites
为false
.
{ "from":"/api/*", "to":"../../../*" }
Run Code Online (Sandbox Code Playgroud)
现在你可以查询http://localhost:5984/api/x?key=val
或http://localhost:5984/api/x/_changes?since=5
.(您无法使用参数查询根URL - 它仍然是错误,但在交通较少的地方.)
以下是最初的终端会话:
$ 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}
Run Code Online (Sandbox Code Playgroud)