簡體   English   中英

Phoenix 中的靜態 HTML 頁面

[英]Static HTML page in Phoenix

我正在嘗試為 Elm 構建一個 JSON 后端。 我只想提供一頁 - elm.html,一個 js 文件 - Main.js - 和一個 css 文件。

我嘗試按照這些說明進行操作,但不足以幫助像我這樣的新手。

所以現在我有了 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)

好的,所以根據 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)

這是一個解決方案,它也為對根 url 的請求提供靜態頁面,即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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM