Sinatra - 我如何获取服务器的域名 [英] Sinatra - how do I get the server's domain name

查看:86
本文介绍了Sinatra - 我如何获取服务器的域名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 看起来很有希望,但我不知道如何从我的应用程序中获取它 - 如何在我的 Ruby 代码中从 Rack 获取内容?

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.

有什么提示吗?

推荐答案

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

simply use request.host inside your code.

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

这篇关于Sinatra - 我如何获取服务器的域名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆