Erlang 是否有 Sinatra 风格的 Web 框架? [英] Is there a Sinatra style web framework for Erlang?

查看:35
本文介绍了Erlang 是否有 Sinatra 风格的 Web 框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 Ruby 和 Rails 编程了很长时间,然后我爱上了 Sinatra 框架的简单性,它允许我构建单页 Web 应用程序.

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.

是否有像 Sinatra 这样的 Web 框架可用于 Erlang?我试过 Erlyweb,但它似乎太重了.

Is there a web framework like Sinatra available for Erlang? I tried Erlyweb but it seems far too heavyweight.

推荐答案

你可以用 mochiweb 实现一些最小的东西:

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}).

如果您需要高级路由,则必须使用正则表达式而不是简单的 case 语句.

If you need advanced routing, you will have to use regex's instead of a simple case statement.

这篇关于Erlang 是否有 Sinatra 风格的 Web 框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆