Liberty Lover
Liberty Lover

Reputation: 1012

Render static HTML with assets folder in Elixir

I have an Elixir project generated with: mix new . --sup httpserver

This follows a short video found here.

This is a very simplistic plain text "hello world" (plus a little). I wanted to see if I could serve static pages (e.g. index.html) with assets located in an assets folder co-located with the index.html.

If I put the index.html and assets in the root of the project, I can send the index.html with the following command:

send_file(conn, 200, "index.html", 0, :all)

However—I do not yet see how to send the asset files when the follow-on asset requests arrive. Can someone give me that direction?

Upvotes: 1

Views: 462

Answers (1)

Adam Millerchip
Adam Millerchip

Reputation: 23137

You can use Plug.Static:

Put the following in your router before plug :match (in a Phoenix project, the endpoint is a better place):

plug Plug.Static, at: "/", from: "assets"

From the docs:

  • :at - the request path to reach for static assets. It must be a string.
  • :from - the file system path to read static assets from.

Upvotes: 2

Related Questions