Easily run an http server from terminal
If available in Hex, this escript can be installed by running this command
mix escript.install hex elixir_server
Or directly from Github like this:
mix escript.install git https://github.com/vikram25897/elixir_server
You can run it like this:
elixir_server --port 4000 --host any index.html
or
elixir_server --p 4000 -h any index.html
host
supports two values:
any
to bind on0.0.0.0
loopback
to bind on127.0.0.1
, defaults toloopback
port
defaults to 4000
If no entrypoint file is provided, elixir_server
searches for any of the following files in order:
index.html
index.exs
index.ex
Will fail if none of these 3 files are provided.
An elixir file path like index.ex
or homepage.exs
can be passed instead of html
filepath.
When passing in an elixir file, make sure that:
- It contains exactly one module.
- That module either has public both
assigns/1
andtemplate_path/1
function or a singleraw_html/1
function. assigns/1
is expected to return a keyword list, like[name: "John Doe", age: 25]
template_path/1
is expected to return path of a template file. You can use EEx template syntax and assigns returned fromassigns/1
function.raw_html
expects you to return a html string.
Instead of running an http server, you can instead generate an html file by passing --only_generate
or -o
flag like this:
elixir_server -o index.html output.html
If only one path is provided like this:
elixir_server -o output.html
It would be considered the output path and it would search for valid entrypoint as explained in Running Webserver
section.