【问题标题】:Static HTML page in PhoenixPhoenix 中的静态 HTML 页面
【发布时间】:2016-09-23 03:06:54
【问题描述】:

我正在尝试为 Elm 构建 JSON 后端。我只想提供一页 - elm.html、一个 js 文件 - Main.js - 和一个 css 文件。

我尝试了following these instructions,但那里不足以帮助像我这样的新手。

所以现在我有了 router.ex

  scope "/", JwtExample do
    pipe_through :browser # Use the default browser stack

    get "/elm", RootController, :index
    get "/", PageController, :index
  end


  # Other scopes may use custom stacks.
  scope "/api", JwtExample do
    pipe_through :api

    resources "/users", UserController, except: [:new, :edit]
  end

这个控制器

defmodule JwtExample.RootController do
  use JwtExample.Web, :controller

  plug :action

  def index(conn, _params) do
    redirect conn, to: "/elm.html"
  end
end

还有我在web/staticpriv/static 中的文件

但是当我浏览到 /elm 时,我得到了

找不到 GET /elm.html (JwtExample.Router) 的路由

【问题讨论】:

标签: html json phoenix-framework elm


【解决方案1】:

好的,所以根据 psantos 的回答,我需要将 lib/endpoint.ex 更改为读取

  plug Plug.Static,
    at: "/", from: :jwt_example, gzip: false,
    only: ~w(css fonts images js favicon.ico robots.txt elm.html)

【讨论】:

    【解决方案2】:

    这是一个解决方案,它还为对根 url 的请求提供静态页面服务,即。 e. https://myapp.test/:

    这是一个将请求映射到 index.html 的根路径的解决方案,它带有一个简短的功能插件,可以添加到您的 endpoint.ex 而无需涉及控制器。它通过定义一个简短的 Plug 函数来更改请求的路径。我希望与在控制器中执行它相比,在端点执行它要快一些。

    def redirect_index(conn = %Plug.Conn{path_info: []}, _opts) do
        %Plug.Conn{conn | path_info: ["elm.html"]}
    end
    
    def redirect_index(conn, _opts) do
        conn
    end
    
    plug :redirect_index
    
    # This is Phoenix's standard configuration of Plug.Static with
    # index.html added.
    
    plug Plug.Static,  
        at: "/", from: :phoenix_elm_starter_template, gzip: false,
        only: ~w(css fonts elm.html images js favicon.ico robots.txt)
    

    请注意,在生产环境中,您通常会在应用服务器层处理静态资产,可能根本不会用到 Phoenix。

    【讨论】:

      猜你喜欢
      • 2015-07-07
      • 1970-01-01
      • 2016-10-19
      • 1970-01-01
      • 2012-04-15
      • 1970-01-01
      • 2012-09-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多