DEV Community

A very simple scriptable Elixir server with tests

Marcio Lopes de Faria on September 19, 2021

Sometimes I miss the old days of Sinatra. It was so simple to build a really simple server, that we can do it in less than ten lines into two files...
Collapse
 
standelethan profile image
Ethan Standel

As far as simple single code file servers go, I really don't think it gets more basic than Express tbh 🤷‍♂️

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!

Collapse
 
neophen profile image
Mykolas Mankevicius

Nodejs is not very industry pervasive... Php is 70% of the internet. You might think nodejs is pervasive because the majority of loud people use. But they realy make up a very small minority of the internet. What you see is not all there is. And coming here on a Elixir post and telling a person to mention something is such a childish move.
nibblestew.blogspot.com/2017/04/wh...