English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
에란그에서 inets 라이브러리는 Erlang에서 웹 서버를 구축하는 데 사용될 수 있습니다. Erlang에서 웹 프로그래밍에 사용되는 몇 가지 함수를 보도록 하겠습니다. HTTP 서버(httpd라고도 함)를 구현하여 HTTP 요청을 처리할 수 있습니다.
서버는 많은 기능을 구현했습니다. 예를 들어-
안전한 소켓 계층(SSL)
에란그 스크립트 인터페이스(ESI)
일반 네트워크 게이트웨이 인터페이스(CGI)
사용자 인증(미네이스, 데츠 또는 원시 텍스트 데이터베이스를 사용)
通用日志文件格式(支持或不支持disk_log(3))
URL别名
动作映射
目录列表
第一项工作是通过命令启动Web库。
inets:start()
下一步是实现inets库的start函数,以便实现web服务器。
以下是在Erlang中创建Web服务器进程的示例。
-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]).
关于上述程序,需要注意以下几点。
端口号必须是唯一的,不能被任何其他程序使用。将在这个端口号上启动 httpd 服务。
server_root和document_root是强制性的参数。
以下是上述程序的输出。
{ok,<0.42.0>}
要在 Erlang 实现 Hello world web 服务器,请执行以下步骤-
단계 1 −实施以下代码−
-module(helloworld). -export([start//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>" ]).
단계 2−위와 같이 코드를 실행하세요. 위의 파일을 컴파일한 후erl에서 다음 명령어를 실행하세요。
c(helloworld).
다음은 다음과 같은 출력을 얻습니다.
{ok,helloworld}
다음 명령어는-
inets:start().
다음은 다음과 같은 출력을 얻습니다.
ok
다음 명령어는-
helloworld:start().
다음은 다음과 같은 출력을 얻습니다.
{ok,<0.50.0>}
단계 3−당신은 지금 url에 접근할 수 있습니다- http://localhost:8081/erl/hello_world:service。