I was looking for examples on how to make a hello world web page using erlang, and since I couldn’t find it I made it myself. for this example we are going to use mochiweb and simplebridge

mochiweb: MochiWeb is an Erlang library for building lightweight HTTP servers.

simplebridge: SimpleBridge takes the pain out of coding to multiple Erlang HTTP servers by creating a standardized interface. It currently supports Mochiweb, Inets, and Misultin, with Yaws coming soon.

to use them we need to have them installed, so we are going to start downloading the code and compiling

# we create a folder for the example mkdir erlweb cd erlweb # download simplebridge git clone http://github.com/rklophaus/SimpleBridge.git # compile it cd SimpleBridge/ make # back to the root directory cd .. # download mochiweb svn checkout http://mochiweb.googlecode.com/svn/trunk/ mochiweb # compile it cd mochiweb/ make # back to the root directory cd ..

we have the needed components, let’s start with the example. create a hello.erl file and paste the code in it:

-module(hello). -export([start/0, loop/1]). start() -> Options = [{ip, "127.0.0.1"}, {port, 8000}], Loop = fun loop/1, mochiweb_http:start([{name, mochiweb_example_app}, {loop, Loop} | Options]). response(Req, Root) -> simple_bridge:make_response(mochiweb_response_bridge, {Req, Root}). response_ok(Req, Root, ContentType, Data) -> Response = response(Req, Root), Response1 = Response:status_code(200), Response2 = Response1:header("Content-Type", ContentType), Response3 = Response2:data(Data), Response3:build_response(). loop(Req) -> HTML = ["<h1>Hello, World!</h1>"], response_ok(Req, "./wwwroot", "text/html", HTML).

now open an erlang shell including the paths for the modules that we are going to use (mochiweb and simplebridge)

erl -pa ./mochiweb/ebin/ -pa ./SimpleBridge/ebin/

in the shell:

Eshell V5.7.4 (abort with ^G) 1> c(hello). {ok,hello} 2> hello:start(). {ok,<0.42.0>}

now we open our browser pointing to http://localhost:8000/ and we should see the hello world obviously I’m going to make an efene version ;) but this time using ifene (the only difference with efene is that blocks are delimited using indentation instead of curly brackets)

@public start = fn () Options = [(ip, "127.0.0.1"), (port, 8000)] Loop = fn loop:1 mochiweb_http.start([(name, mochiweb_example_app), (loop, Loop) : Options]) response = fn (Req, Root) simple_bridge.make_response(mochiweb_response_bridge, (Req, Root)) response_ok = fn (Req, Root, ContentType, Data) Response = response(Req, Root) Response1 = Response.status_code(200) Response2 = Response1.header("Content-Type", ContentType) Response3 = Response2.data(Data) Response3.build_response() @public loop = fn (Req) HTML = ["<h1>Hello, World!</h1>"] response_ok(Req, "./wwwroot", "text/html", HTML)

we save it as hello.ifn and compile the code

fnc hello.ifn

we run the example from an erlang shell (it’s easier;)

erl -pa ./mochiweb/ebin/ -pa ./SimpleBridge/ebin/

in the shell:

Eshell V5.7.4 (abort with ^G) 1> hello:start(). {ok,<0.42.0>}

now we open our browser pointing to http://localhost:8000/ and we should see the hello world