如何通过 ngrok 访问子域?

How can I access a subdomain through ngrok?

我有一个带有 "api" 子域的 rails 站点。我本地机器上的路由如下所示:

http://mysite.dev         #<-- normal web stuff
http://api.mysite.dev     #<-- my api

如何映射这两个子域?这是我的 ngrok 配置文件,但 api 端点似乎指向基本域。

tunnels:
web:
    subdomain: "my-project"
    proto:
        http: mysite.dev:5000
api:
    subdomain: "api.my-project"
    proto:
        http: api.mysite.dev:5000  

如果您在路线中使用约束,我建议使用如下约束 class:

class APIConstraint

  def matches?(request)
     # I would extract the hard coded domains out into some config
     # file, but you get the idea..
     request.host == "ngrok.com" ? request.subdomain.include?("api") : request.subdomain == "api"
  end

end

然后在你的routes.rb

namespace :api do
  constraints APIConstraint.new do
    resources :some_resource
  end
end