37

Using the default Erlang installation what is the minimum code needed to produce a "Hello world" producing web server?

askedFeb 5, 2010 at 11:48
yazz.com's user avatar
2
  • How is this different fromstackoverflow.com/questions/2084639/… ?CommentedFeb 7, 2010 at 16:58
  • 1
    @Zed, Zubair is asking for a minimal "hello world" server--it has a single "page" because it is minimal. The question you linked to asks how to make a functional Web app (one-page, but responds to a variety of requests in different ways). I assume the latter will be ajax-y.CommentedDec 6, 2012 at 13:53

6 Answers6

58

Taking "produce" literally, here is a pretty small one. It doesn't even read the request (but does fork on every request, so it's not as minimal possible).

-module(hello).-export([start/1]).start(Port) ->    spawn(fun () -> {ok, Sock} = gen_tcp:listen(Port, [{active, false}]),                     loop(Sock) end).loop(Sock) ->    {ok, Conn} = gen_tcp:accept(Sock),    Handler = spawn(fun () -> handle(Conn) end),    gen_tcp:controlling_process(Conn, Handler),    loop(Sock).handle(Conn) ->    gen_tcp:send(Conn, response("Hello World")),    gen_tcp:close(Conn).response(Str) ->    B = iolist_to_binary(Str),    iolist_to_binary(      io_lib:fwrite(         "HTTP/1.0 200 OK\nContent-Type: text/html\nContent-Length: ~p\n\n~s",         [size(B), B])).
answeredFeb 8, 2010 at 3:07
Felix Lange's user avatar
Sign up to request clarification or add additional context in comments.

6 Comments

See{packet, http} forOptions ofgen_tcp:listen for decoding HTTP headers.
I was looking at this code, and even though it looks clean, I found an issue. When testing it with Apache Bench, I get aapr_socket_recv: Connection reset by peer (104). it works fine when accessing in a browser, or using curl. Do you have any idea why this is?
@MartinKristiansen Remove the linegen_tcp:close(Conn).
@FelixLange Thanks for this answer Felix.. Could you please explain the code written. It will be very helpful for erlang enthusiasts like me :-)
The lastiolist_to_binary/1 inresponse/1 is unnecessary.
|
11

For a web server using only the built in libraries check outinets http_server.When in need of some more power but still with simplicity you should check out themochiweb library. You can google for loads of example code.

Vadim Kotov's user avatar
Vadim Kotov
8,2848 gold badges51 silver badges63 bronze badges
answeredFeb 5, 2010 at 12:17
Jon Gretar's user avatar

1 Comment

Thats great, so using INets can I write a single module webserver?
8

Another way, similar to thegen_tcp example above but with less code and already offered as a suggestion, is using theinets library.

%%%%%% A simple "Hello, world" server in the Erlang.%%%-module(hello_erlang).-export([  main/1,  run_server/0,  start/0]).main(_) ->  start(),  receive    stop -> ok  end.run_server() ->  ok = inets:start(),  {ok, _} = inets:start(httpd, [    {port, 0},    {server_name, "hello_erlang"},    {server_root, "/tmp"},    {document_root, "/tmp"},    {bind_address, "localhost"}  ]).start() -> run_server().

Keep in mind, this exposes your/tmp directory.

To run, simply:

$ escript ./hello_erlang.erl
answeredJan 16, 2018 at 19:02
spacez320's user avatar

Comments

7

Do you actually want to write a web server in Erlang, or do you want an Erlang web server so that you can create dynamic web content using Erlang?

If the latter, tryYAWS. If the former, have a look at theYAWS source code for inspiration

answeredFeb 5, 2010 at 11:55
Paul Butcher's user avatar

1 Comment

I want to make dynamic content, but just wanted to know the bare minimum needed for a web server. I looked at the Yaws source code and my first impression was that alot of code was needed.
4

For a very easy to use webserver for building restful apps or such check out the gen_webserver behaviour:http://github.com/martinjlogan/gen_web_server.

answeredFeb 25, 2010 at 22:11
Tristan Sloughter's user avatar

Comments

1

Just one fix for Felix's answer and it addresses the issues Martin is seeing. Before closing a socket, all data being sent from the client should be received (using for exampledo_recv fromgen_tcp description).

Otherwise there's a race condition for the browser/proxy sending the HTTP request being quick enough to send the http request before the socket is closed.

answeredAug 24, 2012 at 17:19
typingduck's user avatar

Comments

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.