如何在erlang-cowboy中使用post并获取处理程序

时间:2014-05-21 07:04:26

标签: erlang httphandler cowboy erl

请帮助我创建一个新项目,即,使用erlang-cowboy登录页面创建登录页面,当用户输入数据并点击浏览器中的提交按钮时,用户的详细信息应包含用户名,密码和提交按钮保存在服务器中为我创建了一个文件toppage_handler.erl但是当我输入make命令错误正在提升

错误:

root@ErlangCowboy:~/cowboy/examples/practice_world# make
make[1]: Entering directory `/root/cowboy/examples/practice_world/deps/cowboy'
make[2]: Entering directory `/root/cowboy/examples/practice_world/deps/cowlib'
 APP    cowlib.app.src
make[2]: Leaving directory `/root/cowboy/examples/practice_world/deps/cowlib'
make[2]: Entering directory `/root/cowboy/examples/practice_world/deps/ranch'
 APP    ranch.app.src
make[2]: Leaving directory `/root/cowboy/examples/practice_world/deps/ranch'
 APP    cowboy.app.src
make[1]: Leaving directory `/root/cowboy/examples/practice_world/deps/cowboy'
 ERLC   practice_world_app.erl practice_world_sup.erl toppage_handler.erl
src/toppage_handler.erl:14: syntax error before: '{'
src/toppage_handler.erl:4: function handle/2 undefined
make: *** [ebin/practice_world.app] Error 1

和toppage_handler.erl文件是:

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

init(_Transport, Req, []) ->
    {ok, Req, undefined}.

handle(Req, State) ->
    {Method, Req2} = cowboy_req:method(Req),
    case Method of
        <<"POST">> ->
            Body = <<"<h1>This is a response for POST</h1>">>
            {ok, Req3} = cowboy_req:reply(200, [], Body, Req3),
            {ok, Req3, State};
        <<"GET">> ->
            Body = <<"<h1>This is a response for GET</h1>">>
            {ok, Req3} = cowboy_req:reply(200, [], Body, Req3),
            {ok, Req3, State};
        _ ->
            Body = <<"<h1>This is a response for other methods</h1>">>
            {ok, Req3} = cowboy_req:reply(200, [], Body, Req3),
            {ok, Req3, State}
    end.

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

1 个答案:

答案 0 :(得分:0)

错误消息表明您的{ok, Req3} = ...行在{之前有语法错误。由于{是该行中的第一个重要字符,因此我们可以合理地假设错误实际上在之前

实际上,错误源于这一行:

Body = <<"<h1>This is a response for POST</h1>">>

实际上在其他两个案例中都重复了。在每个case子句的第一行中,您缺少表达式终止符,。这些行应该是:

Body = <<"<h1>This is a response for POST</h1>">>,
{ok, Req3} = ...