Sinatra - 如何获取服务器的域名

发布于 2024-11-07 04:05:47 字数 269 浏览 6 评论 0原文

我正在尝试在我的 Sinatra 应用程序中获取域名,但作为新手,我真的很难弄清楚如何做到这一点,而且我知道这一定是可能的!

Rack::Request#host_with_port 看起来很有希望,但我不知道如何从我的应用程序中获取它 - 如何在 Ruby 代码中从 Rack 获取内容?

或者还有另一种方法 - 我想我并不是真的想在每次请求发生时都这样做(虽然这还不错),但我认为如果我可以在应用程序时只执行一次会更好加载完毕。

有什么提示吗?

I'm trying to get the domain name in my Sinatra app but as a newbie I really am struggling to figure out how to do this, and I know it must be possible!

Rack::Request#host_with_port looks promising, but I don't know how to get this from my app - how do I get stuff from Rack in my Ruby code?

Or is there another way - I'm thinking I don't really want to do this every time a request happens (although it's not too bad), but I thought it'd be better if I could just do it once when the application loads up.

Any hints?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

<逆流佳人身旁 2024-11-14 04:05:47

只需在代码中使用 request.host 即可。

get  "/" do
  puts request.host #=> localhost
end

simply use request.host inside your code.

get  "/" do
  puts request.host #=> localhost
end
屋顶上的小猫咪 2024-11-14 04:05:47

看一下:

request.env.inspect

这样就可以看到所有的请求环境变量了。

我认为你正在寻找

request.env["SERVER_NAME"]

Take a look at:

request.env.inspect

so you can see all the request environment variables.

I think that you are looking for

request.env["SERVER_NAME"]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文