Using the default Erlang installation what is the minimum code needed to produce a "Hello world" producing web server?
Web server hardware is connected to the internet and allows data to be exchanged with other connected devices, while web server software controls how a user accesses hosted files. The web server process is an example of the client/server model. All computers that host websites must have web server software.
It is a common misconception that to build a web server at home, you need to deal with costs and complications. However, the process of building the server is not complicated at all, and you can use old hardware that you have around the house! The only cost you'll have is the electricity bill.
A web server is the computer on which the web pages of your website are stored, who delivers or 'serves' the content of your website to the users through Internet. Simply we can say a computer program that distribute web pages as they are requested requests via HTTP.
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])).
For a web server using only the built in libraries check out inets http_server. When in need of some more power but still with simplicity you should check out the mochiweb library. You can google for loads of example code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With