如何在没有Phoenix的情况下配置Plug.Static [英] How to configure the Plug.Static without Phoenix

查看:116
本文介绍了如何在没有Phoenix的情况下配置Plug.Static的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何在没有任何其他框架(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

  1. Plug.Static的声明是否在正确的位置?不应该在plug :dispatch之后吗?
  2. 我是否需要定义其他路线
  3. 使用以下声明:
  1. Is the declaration of Plug.Static at the right place ? Shouldn't it be after plug :dispatch ?
  2. Do I need to define an additional route
  3. With this declaration:
  1. 要访问的URL是什么,例如index.html?
  2. 文件系统index.html上应位于的位置
  1. what is the URL to reach, say index.html?
  2. 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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