问题描述
我试图弄清楚如何在没有任何其他框架(Phoenix,Sugar等)的情况下 Static 配置;只是牛仔,即插即用药.我只是不知道如何将这些东西放到路由器中.
I am trying to figure out how to configure Plug.Static without any other framework (Phoenix, Sugar, etc); just Cowboy, Plug and Elixir. I just don't know how to put things together in the Router.
plug :match
plug Plug.Static, at: "/pub", from: :cerber
plug :dispatch
get "/" do
Logger.info "GET /"
send_resp(conn, 200, "Hello world\n")
end
-
Plug.Static
的声明是否在正确的位置?不应该在plug :dispatch
之后吗? - 我是否需要定义其他路线
- 使用以下声明:
- Is the declaration of
Plug.Static
at the right place ? Shouldn't it be afterplug :dispatch
? - Do I need to define an additional route
- With this declaration:
- 要访问的URL是什么,例如
index.html
? - 文件系统
index.html
上应位于的位置
- what is the URL to reach, say
index.html
? - where on the file system
index.html
should be located
我只是失去了.
I'm just lost.
推荐答案
看看 Plug.Router文档了解:match
和:dispatch
的工作方式. :match
将尝试查找匹配的路由,而:dispatch
将调用它.这意味着仅当路由器中有匹配的路由时,才会调用设置中的Plug.Static
,这没有任何意义.您想在所有内容之前先plug Plug.Static
.请记住,插件只是按声明顺序调用的函数.
Take a look at Plug.Router docs for how :match
and :dispatch
works. :match
will try to find a matching route and :dispatch
is going to invoke it. This means Plug.Static
in your setup will only be invoked if you have a matching route in your router, which doesn't make sense. You want plug Plug.Static
before everything. Remember plugs are just functions that are invoked in the order they are declared.
除此之外,您的Plug.Static设置似乎还可以.您当前的配置将在"/pub"提供资产,这意味着"/pub/index.html"将在您的应用中查找"priv/static/index.html".此处的更多信息: http://hexdocs.pm/plug/Plug.Static.html
Other than that, your Plug.Static setup seems ok. Your current configuration will serve assets at "/pub", meaning "/pub/index.html" will look for "priv/static/index.html" in your app. More info here: http://hexdocs.pm/plug/Plug.Static.html
这篇关于如何在没有Phoenix的情况下配置Plug.Static的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!