【问题标题】:init/2 undefined in cowboy hello_handlerinit/2 在牛仔 hello_handler 中未定义
【发布时间】:2017-04-14 03:01:07
【问题描述】:

我关注https://ninenines.eu/docs/en/cowboy/2.0/guide/getting_started/

当我跑步时

gmake new t=cowboy_http n=hello_handler

这会创建 src/hello_handler

-module(hello_handler).
-behaviour(cowboy_http_handler).

-export([init/3]).
-export([handle/2]).
-export([terminate/3]).

-record(state, {
}).

init(_, Req, _Opts) ->
    {ok, Req, #state{}}.

handle(Req, State=#state{}) ->
    {ok, Req2} = cowboy_req:reply(200, Req),
    {ok, Req2, State}.

terminate(_Reason, _Req, _State) ->
    ok.

说明说要修改 init/2 但只有 init/3。该文档是否已过时..?

【问题讨论】:

  • 看起来erlang.mk 已经过时了。link
  • 谢谢!这对我有用..

标签: erlang cowboy


【解决方案1】:

您正在查看的文档与您正在查看的牛仔版本不匹配。文档适用于 2.x 版,处理程序适用于 1.x 版。

Cowboy 1.x 系列实现了一个带有行为的 http 处理程序。当您从模板生成处理程序时,该处理程序会实现该行为。

然而,在 Cowboy 2.x 中,处理程序只需要实现init/2,不再需要实现该行为。

当您查看 Cowboy 时,您可以使用 make docs 为它制作文档。然后您可以在doc 中本地浏览它们。这样,您就可以保证拥有与您的牛仔版本相匹配的文档。

【讨论】:

    猜你喜欢
    • 2020-09-28
    • 2017-07-26
    • 2016-08-02
    • 2014-04-01
    • 2013-05-30
    • 2013-06-08
    • 2016-07-19
    • 2014-07-17
    • 2019-05-09
    相关资源
    最近更新 更多