English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In Erlang kann die Bibliothek inets verwendet werden, um Webserver in Erlang zu erstellen. Lassen Sie uns einige Funktionen betrachten, die in Erlang für Web-Programmierung verwendet werden. Ein HTTP-Server (auch als httpd bezeichnet) kann implementiert werden, um HTTP-Anfragen zu verarbeiten.
Der Server hat viele Funktionen implementiert, wie z.B.-
SicherheitsSocket-Schicht (SSL)
Erlang-Skript-Schnittstelle (ESI)
Gemeinsamer Gateway-Interface (CGI)
Benutzeridentifikation (verwendet Mnesia, Dets oder reinen Textdatenbanken)
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 through a 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.
-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.
Here is the output of the above program:
{ok,<0.42.0>}
To implement a Hello World web server in Erlang, please follow these steps:-
Schritt 1 −Implement 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, [ "Inhalt-Typ: text/html\r\n\r\n", "<html><body>Hello, World!</body></html>" ]).
Schritt 2−Führen Sie den folgenden Code aus. Kompilieren Sie die obige Datei und führen Sie ihn dann inerlführen Sie den folgenden Befehl aus.
c(helloworld).
Sie erhalten die folgenden Ausgaben.
{ok,helloworld}
Der nächste Befehl ist-
inets:start().
Sie erhalten die folgenden Ausgaben.
ok
Der nächste Befehl ist-
helloworld:start().
Sie erhalten die folgenden Ausgaben.
{ok,<0.50.0>}
Schritt 3−Sie können jetzt die URL aufrufen- http://localhost:8081/erl/hello_world:service.