新手问题 如何解决 Grape API 的 url 在少了一个 / 的情况下居然还能工作的问题

seaify · 2015年09月10日 · 最后由 fumesy 回复于 2015年09月12日 · 1950 次阅读

正常状态: /api/v1/xxx 能工作 BUG: /apiv1/xxx 居然也能工作

mount Api::Dispatch => '/api/'

grape 请求打印出的路由信息

Started GET "/apiv1/screens" for ::1 at 2015-09-10 18:01:38 +0800
{
    "route_info" => #<Grape::Route:0x007fd5e241c9a8 @options={:description=>"Retun screens", :prefix=>nil, :version=>"v1", :namespace=>"/", :method=>"GET", :path=>"/:version/screens(.:format)", :params=>{}, :compiled=>/\A\/(?<version>v1)\/screens(?:\.(?<format>[^\/.?]+))?\Z/}>
}

有没有办法,让http://localhost:3001/apiv1/screens.json访问失败?

实在是看不懂你想说什么

你贴了 4 个一样的地址是什么意思……真看不懂。

#3 楼 @msg7086

就是现在/apiv1/screens.json 能够正确访问到数据,这个是不希望看到的,是否有办法保证仅仅/api/v1/screens.json 能访问到数据,注意中间的"/", 现在 mount 到了/api/, grape 里的版本是 v1, 路径是/screens,

# routes.rb
mount Api::Dispatch => '/'
# api/dispath.rb
prefix '/api'
需要 登录 后方可回复, 如果你还没有账号请 注册新账号