English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Erlang Network Programming

In Erlang, the inets library can be used to build web servers in Erlang. Let's take a look at some functions used for web programming in Erlang. It is possible to implement an HTTP server (also known as httpd) to handle HTTP requests.

The server implements many features, such as-

  • Secure Sockets Layer (SSL)

  • Erlang Script Interface (ESI)

  • Common Gateway Interface (CGI)

  • User authentication (using Mnesia, Dets, or plain text database)

  • General log file format (supported or not supported by disk_log(3))

  • URL alias

  • Action mapping

  • Directory listing

The first task is to start the web library via command.

inets:start()

The next step is to implement the start function of the inets library to implement the web server.

The following is an example of creating a web server process in Erlang.

For example

-module(helloworld). 
-export([start/0]). 
start() ->
   inets:start(), 
   Pid = inets:start(httpd, [{port, 8081}, {server_name,"httpd_test"}, 
   {server_root,"D://tmp"},{document_root,"D://tmp/htdocs"},
   {bind_address, "localhost"}]), io:fwrite("~p",[Pid]).

The following points should be noted about the above program:

  • the port number must be unique and cannot be used by any other program. The httpd service will be started on this port.

  • server_rootanddocument_rootis a mandatory parameter.

Output

The following is the output of the above program.

{ok,<0.42.0>}

To implement a Hello World web server in Erlang, please follow these steps:-

Step 1 −Implement the following code−

-module(helloworld). 
-export([start/0,service/3]). 
start() ->
   inets:start(httpd, [ 
      {modules, [ 
         mod_alias, 
         mod_auth, 
         mod_esi, 
         mod_actions, 
         mod_cgi, 
         mod_dir,
         mod_get, 
         mod_head, 
         mod_log, 
         mod_disk_log 
      ]}}, 
      
      {port,8081}, 
      {server_name,"helloworld"}, 
      {server_root,"D://tmp"}, 
      {document_root,"D://tmp/htdocs"}, 
      {erl_script_alias, {"/erl", [helloworld]}}, 
      {error_log, "error.log"}, 
      {security_log, "security.log"}, 
      {transfer_log, "transfer.log"}, 
      
      {mime_types,[ 
         {"html","text}/html"}, {"css","text"}/css"}, {"js","application/x-javascript"} ]} 
   ]). 
         
service(SessionID, _Env, _Input) -> mod_esi:deliver(SessionID, [ 
   "Content-Type: text/html\r\n\r\n", "<html><body>Hello, World!</body></html>" ]).

Step 2−Run the code as follows. Compile the above file and then runerlrun the following command in

c(helloworld).

You will get the following output.

{ok,helloworld}

The next command is-

inets:start().

You will get the following output.

ok

The next command is-

helloworld:start().

You will get the following output.

{ok,<0.50.0>}

Step 3−You can now access the url- http://localhost:8081/erl/hello_world:service.