2

How can one do routing in Yaws? Like routing in ASP.NET MVC or Rails.

Of-course Erlang is a functional language without notions from oo world; but one could rout http://[domain].[com]/controller/action/arg1/arg2/ as a GET request to a module named 'controller' with an 'action/2' function (or is there already such a lib).

4

2 回答 2

4

Yaws 提供了几种方法来做到这一点:

  1. 如this answer所示,appmod在路径“/”上注册一个,但正如那里的问题所暗示的那样,这种方法不允许您摆脱 URL 上的“.yaws”后缀。
  2. 使用重写模块,如Yaws PDF 文档的第 7.1.2 节所示。这将允许您的客户使用没有“.yaws”后缀的 URL——您的重写模块可以在需要的地方添加它们。
  3. 使用Yaws PDF 文档第 52 页所述的调度模块。请注意,尽管这种方法绕过了许多有用的 Yaws 调度机制,包括.yaws页面处理,所以只有在您真正知道自己在做什么时才使用它。

在这 3 个选择中,我相信 rewrite 模块是解决这个特定问题的最佳选择。

于 2013-12-09T03:18:38.403 回答
0

我不知道是否有这样的模块,但我没有看到这样做的模块的好处:

out(Arg) ->
    Uri = yaws_api:request_url(Arg),
    Path = string:tokens(Uri#url.path, "/"),
    Method = (Arg#arg.req)#http_request.method,
    out(Arg, Method, Path).

out(_Arg, 'GET', [Module, Function | Args]) ->
    apply(Module, Function, Args).

有一些错误处理等等。顺便说一句,如何被黑客入侵的好方法。

于 2013-12-08T17:51:31.357 回答