mikejm

mikejm

Simple Bandit HTTP Server test code giving "econnaborted" error?

I have started a basic HTTP Server as per the following tutorials:

https://hexdocs.pm/plug/readme.html
https://hexdocs.pm/bandit/Bandit.html

#//PLUG MODULE FOR BANDIT  https://hexdocs.pm/plug/readme.html
defmodule MyPlug do
  import Plug.Conn

  def init(options) do
    # initialize options
    options
  end

  def call(conn, _opts) do
    conn
    |> put_resp_content_type("text/plain")
    |> send_resp(200, "Hello world")
  end
end

#//MAIN MODULE
defmodule ServerTest do

def startHttpServer do
    IO.puts("Try Start HTTP Server");
    webserver = {Bandit, plug: MyPlug, scheme: :http, port: 4000 }
    supervisorOpts = [strategy: :one_for_one ]
    {:ok, _} = Supervisor.start_link([webserver], supervisorOpts)
    IO.puts("Plug now running on localhost:4000");
    #Process.sleep(:infinity)
  end

end

This works, in that if I run the project and then ServerTest.startHttpServer() and go to localhost:4000 I get a “Hello world” back. But I am also getting this:

21:11:37.278 [error] GenServer #PID<0.554.0> terminating
** (stop) :econnaborted
Last message: {:tcp_error, #Port<0.3>, :econnaborted}
State: {%ThousandIsland.Socket{socket: #Port<0.3>, transport_module: ThousandIsland.Transports.TCP, read_timeout: 60000, silent_terminate_on_error: false, span: %ThousandIsland.Telemetry{span_name: :connection, telemetry_span_context: #Reference<0.267163659.1006895106.157280>, start_time: -5764607406509056, start_metadata: %{telemetry_span_context: #Reference<0.267163659.1006895106.157280>, parent_telemetry_span_context: #Reference<0.267163659.1006895106.157220>, remote_address: {127, 0, 0, 1}, remote_port: 63311}}}, %{opts: %{http: [], http_1: [], http_2: [], websocket: []}, plug: {MyPlug, []}, handler_module: Bandit.HTTP1.Handler, http_1_enabled: true, http_2_enabled: true, requests_processed: 2}}

21:13:32.629 [error] ** (Bandit.HTTPError) Header read socket error: :closed

What does this all mean? What is wrong with the above code? Thanks for any help.

I find it funny also because I haven’t started any GenServer, so I presume this is a GenServer being started by Plug/Bandit? What is the problem?

Where Next?

Popular in Questions Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
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
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
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
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
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
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New

We're in Beta

About us Mission Statement