DEV Community

Discussion on: A very simple scriptable Elixir server with tests

Collapse
 
kelvinst profile image
Kelvin Stinghen

Well, in Elixir's defense, please note this script includes the server AND the tests for it, the server per se could be done just with:

Mix.install([{:plug_cowboy, "~> 2.0"}])

defmodule Server do
  use Plug.Router
  plug(:match)
  plug(:dispatch)

  get "/hello-world" do
    send_resp(conn, 200, "Hello World!")
  end

  match _ do
    send_resp(conn, 404, "Nothing to see here")
  end
end

Plug.Cowboy.http(Server, [], port: 4000)
System.no_halt(true)
Enter fullscreen mode Exit fullscreen mode

Also, this is a totally standalone script, it does not require ANYTHING else besides you having Elixir installed and calling elixir server.exs, as the dependencies are downloaded for you with the function Mix.install on the beginning of the file, so you could totally just download that script and run it. While with express, AFAIR you need to install express too, which either involves a npm -g express (that might lead to problems with different express versions) or having a folder structure and a package.json, which is not that simple TBH.

Collapse
 
standelethan profile image
Ethan Standel

Definitely good points! I suppose my suggestion would have to include something unsavory like exec("npm I express@latest -g") to truly get it into a true single file that you can run with just Node installed which just... yeah, yuck.

I guess I could make that point that Deno has this functionality built in if I wanted to be that kind of JS/TS fanboy but then we're out of the "simplistic" and well documented context.

For the record, I dig Elixir + Phoenix. I'm not a Node dev strictly (though it's what clients have most commonly been asking for, in my experience), I just think it's the defacto for most simple tooling.

Thread Thread
 
kelvinst profile image
Kelvin Stinghen

Yeah, deno is a nice project, hopefully it gets traction as it solves a lot of the problems node has. But I'm sticking with Elixir either way. 😄

Your comment got me thinking though: is this something we want? I mean, is there a valid use case for a very simple server like this? Cause if there is, maybe there is room for another library on top of plug that would make all this simpler. Maybe even improved tooling around single-file scripts servers IDK 🤔

Thread Thread
 
standelethan profile image
Ethan Standel

I think there's a use-case for incredibly simple tooling. I mean... the Docker build for deploying this server to production just has to have Elixir installed and that's pretty cool!