janderland

janderland

Plug/Cowboy2 failing on all HTTP requests

Hello friends,

I’ve been using my free time in the last month or so to learn Elixir in the hope of replacing a Node.js server I wrote for a side project. To really understand the workings of Plug, I wanted to setup an ultra simple Plug/Cowboy HTTP server and start from there. Eventually, I’m sure I’ll move on to Phoenix, but I was really attracted to the simplicity of Plug as a starting point.

I uploaded the simplest “broken” version of my code to this repo. You should be able to easily clone and run that with mix, and I’ll also show the core code below:

defmodule PlugCowboy2Http2 do
  use Application

  def start(_type, _args) do
    children = [
      {
        Plug.Adapters.Cowboy2,
        scheme: :http,
        plug: PlugCowboy2Http2.Router
      }
    ]

    opts = [
      strategy: :one_for_one,
      name: PlugCowboy2Http2.Supervisor
    ]

    {:ok, _} = Supervisor.start_link(children, opts)
  end
end


defmodule PlugCowboy2Http2.Router do
  use Plug.Router

  plug(Plug.Logger)
  plug(:match)
  plug(:dispatch)

  match _ do
    send_resp(conn, 200, "Hello!")
  end
end

When I navigate my browser (Chromium: 70.0.3538.16) to http://localhost:4000 I seem to always crash a process. The browser receives an ERR_CONNECTION_RESET and the Elixir runtime prints the following message to console:

13:11:21.735 [error] Ranch protocol #PID<0.286.0> of listener PlugCowboy2Http2.Router.HTTP (cowboy_clear) terminated
** (exit) an exception was raised:
** (UndefinedFunctionError) function :cowboy_http.init/5 is undefined (module :cowboy_http is not available)
(cowboy) :cowboy_http.init(#PID<0.184.0>, PlugCowboy2Http2.Router.HTTP, #Port<0.5>, :ranch_tcp, %{env: %{dispatch: [{:, [], [{:, , Plug.Adapters.Cowboy2.Handler, {PlugCowboy2Http2.Router, }}]}]}, stream_handlers: [Plug.Adapters.Cowboy2.Stream]})
(stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3

To add to the confusion, when I switch the :scheme option to :https and add a cert & key file, the server works perfectly. Can anyone show me where I’m going wrong?

Marked As Solved

janderland

janderland

I found a possible culprit for the above bugs:

It seems that when using elixir-vim, when Vim attempts to build your project upon saving a file, it can sometimes corrupt the build directory and cause odd problems during execution. To prevent this, I now start Vim via the following command: MIX_ENV=edit vim.

Also Liked

janderland

janderland

I got it working on my machine now. Ran the following commands:

mix deps.clean --all
rm -rf _build
rm mix.lock
mix deps.get
mix compile
mix run --no-halt

I thought I had tried this already, but maybe I had forgotten one of them. Anyhow, it seems to be working now. Thanks for your help.

xlphs

xlphs

Well, I just cloned you repo and ran mix deps.get followed by mix compile and started the app with iex -S mix and I see Hello! in the browser. Are you sure you installed and/or compiled all dependencies correctly? I am most certain your code is correct.

xlphs

xlphs

I’m running Elixir (1.7.3) with Erlang/OTP 21 [erts-10.0] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe]

Mac os x 10.11 :stuck_out_tongue:

OvermindDL1

OvermindDL1

Yeah I’d say wipe your _build directory, then rebuild all to start with.

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
Kagamiiiii
Student &amp; New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New

We're in Beta

About us Mission Statement