Erlang是否有Sinatra风格的网络框架? [英] Is there a Sinatra style web framework for Erlang?
本文介绍了Erlang是否有Sinatra风格的网络框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有一个网络框架,如Sinatra可用于Erlang?我试过Erlyweb,但是它似乎太重了。
解决方案
你可以通过mochiweb来实现一些最小化:
start() - >
mochiweb_http:start([{'ip',127.0.0.1},{port,6500},
{'loop',fun?MODULE:loop / 1}])。
%mochiweb将为每个请求调用循环函数
loop(Req) - >
RawPath = Req:get(raw_path),
{Path,_,_} = mochiweb_util:urlsplit_path(RawPath),%获取请求路径
case基于%在路径
/ - >响应(Req,<< p> Hello World!< / p>>>);
/ a - >响应(Req,<< p> Page a< / p>>>);
...
_ - > $($)
end(Req,<< p> Page not found!< / p>>>
回复(需求,内容) - >
Req:response({200,[{<Content-Type>><text / html>>}],Content})。
如果您需要高级路由,您将不得不使用正则表达式而不是简单的case语句。 p>
I programmed in Ruby and Rails for quite a long time, and then I fell in love with the simplicity of the Sinatra framework which allowed me to build one page web applications.
Is there a web framework like Sinatra available for Erlang? I tried Erlyweb but it seems far too heavyweight.
解决方案
You could achieve something minimal with mochiweb:
start() ->
mochiweb_http:start([{'ip', "127.0.0.1"}, {port, 6500},
{'loop', fun ?MODULE:loop/1}]).
% mochiweb will call loop function for each request
loop(Req) ->
RawPath = Req:get(raw_path),
{Path, _, _} = mochiweb_util:urlsplit_path(RawPath), % get request path
case Path of % respond based on path
"/" -> respond(Req, <<"<p>Hello World!</p>">>);
"/a" -> respond(Req, <<"<p>Page a</p>">>);
...
_ -> respond(Req, <<"<p>Page not found!</p>">>)
end.
respond(Req, Content) ->
Req:respond({200, [{<<"Content-Type">>, <<"text/html">>}], Content}).
If you need advanced routing, you will have to use regex's instead of a simple case statement.
这篇关于Erlang是否有Sinatra风格的网络框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文