elixir - 如何在没有 Phoenix 的情况下配置 Plug.Static

标签 elixir phoenix-framework cowboy

我想弄清楚如何配置 Plug.Static 没有 任何其他框架(Phoenix、Sugar 等);只是 Cowboy、Plug 和 Elixir。我只是不知道如何将东西放在路由器中。

  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之后?
  • 我是否需要定义额外的路由
  • 有了这个声明:
  • 要访问的 URL 是什么,例如 index.html ?
  • 文件系统的位置 index.html应位于

  • 我只是失去了。

    最佳答案

    看看Plug.Router docs对于如何:match:dispatch作品。 :match将尝试找到匹配的路由和 :dispatch将调用它。这意味着 Plug.Static仅当您的路由器中有匹配的路由时才会调用您的设置,这是没有意义的。你要plug Plug.Static在一切之前。请记住,插件只是按照它们声明的顺序调用的函数。

    除此之外,您的 Plug.Static 设置似乎没问题。您当前的配置将在“/pub”中提供 Assets ,这意味着“/pub/index.html”将在您的应用中查找“priv/static/index.html”。更多信息在这里:http://hexdocs.pm/plug/Plug.Static.html

    关于elixir - 如何在没有 Phoenix 的情况下配置 Plug.Static,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32543265/

    相关文章:

    rest - 为 Cowboy REST API 启用 CORS

    unit-testing - Elixir : test has_many association

    elixir - 在 Elixir Phoenix Absinthe GraphIQL 客户端中实现身份验证?

    Elixir /Phoenix : How to do unit tests for models without using Ecto?

    erlang - 在牛仔中找不到包含 lib "rabbit_common/include/rabbit.hrl"

    cowboy - 在不同端口上使用牛仔服务器启动 Phoenix 应用程序

    elixir - 为什么值 rest 包含值 "llo"

    validation - 为什么 Changeset.change 在 Elixir 中跳过验证?

    elixir - Phoenix : Render content_tag as HTML?

    elixir - 如何在 Ecto 中更新一对多关联