2017-04-20 1 views
0

pure Erlang을 사용하면 "hello-world"HTML 페이지를 보여주는 웹 서버의 가장 간단한 구현은 무엇입니까?pure erlang을 사용하여 간단한 hello-world 웹 서버를 작성하는 방법

+3

이것을 읽을 수 있습니다. http://blog.foemmel.com/2008/05/hello-world-webapp-in-erlang.html – marmeladze

+2

Google에서 코드를 작성하고 싶습니다. 당신. 대부분의 사용자는 곤경에 처한 코더 코드를 기꺼이 만들지 만 일반적으로 포스터가 이미 문제를 해결하려고 시도했을 때만 도움이됩니다. 이러한 노력을 입증하는 좋은 방법은 지금까지 작성한 코드, 예제 입력 (있는 경우), 예상 출력 및 실제로 얻은 출력 (콘솔 출력, 역 추적 등)을 포함시키는 것입니다. 더 자세하게 제공할수록 더 많은 답변을받을 수 있습니다. [FAQ]와 [ask]를 확인하십시오. –

+1

얼랭 (Erlang)의 간단한 HTTP 1.0 웹 서버는 약 40 줄의 코드로 주석을 제외하고 있습니다 : [https://gist.github.com/vinoski/4996859](https://gist.github.com/vinoski/4996859)). –

답변

7

erlang에는 web server 길이가 575 자입니다.

$ escript hgolf.erl 

을 실행하지만 표준 얼랑/OTP 분포의 일환으로 더 풍부한 기능의 웹 서버 inets이 방법

$ cat hgolf.erl 

main(_)->{ok,L}=gen_tcp:listen(36895,[]),s(L). 
s(L)->{ok,S}=gen_tcp:accept(L),receive{tcp,S,"GET "++R}->[F|_]=string:tokens("/var/www"++R," "),case case file:read_file_info(F)of{ok,{_,_,regular,read,_,_,_,_,_,_,_,_,_,_}}->a;{ok,_}->"500 Server Error";_->"404 File Not Found"end of a->h(S,"200 OK\r\nContent-Type: "++case lists:reverse(F)of"lmth."++_->"text/html";"txt."++_->"text/plain";_->"application/octet-stream"end,[]),file:sendfile(F,S);E->h(S,E,E)end;_->E="405 Not Supported",h(S,E,E)end,gen_tcp:close(S),s(L). 
h(S,H,B)->gen_tcp:send(S,["HTTP/1.1 ",H,"\r\n\r\n",B]). 

.

관련 문제