2017-04-20 97 views
0

使用純Erlang什麼是最簡單的web服務器實現,顯示「hello-world」HTML頁面?如何用純erlang編寫簡單的hello-world web服務器

+3

看看這個 - http://blog.foemmel.com/2008/05/hello-world-webapp-in-erlang.html – marmeladze

+2

它看起來像你想我們寫一些代碼您。儘管許多用戶願意爲遇險的編碼人員編寫代碼,但他們通常只在海報已嘗試自行解決問題時才提供幫助。證明這一努力的一個好方法是包含迄今爲止編寫的代碼,示例輸入(如果有的話),期望的輸出以及實際獲得的輸出(控制檯輸出,回溯等)。您提供的細節越多,您可能會收到的答案就越多。檢查[FAQ]和[問]。 –

+1

以下是Erlang中一個簡單的HTTP 1.0 Web服務器,大約有40行代碼,不包括評論:[https://gist.github.com/vinoski/4996859](https://gist.github.com/vinoski/4996859 )。 –

回答

7

erlang中有575個字符長web server

$ 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]). 

如何運行

$ escript hgolf.erl 

但有標準的Erlang/OTP發行版的一部分更多的其他功能的Web服務器inets