创建然后访问Mochiweb Cookies

时间:2014-11-24 21:32:33

标签: cookies erlang mochiweb

嘿,我试图让网站向用户发送Cookie,然后显示网页。

所以我发现http://alexmarandon.com/articles/mochiweb_tutorial/是关于如何制作Cookie的唯一真正的教程,但它似乎对我来说是错误的。

我的循环看起来像这样(我的make_cookie,get_cookie_value,render_ok和get_username与他相同,除了我使用&#39; mename&#39;作为键而不是&#39; username&#39;):< / p>

loop(Req, DocRoot) ->
"/" ++ Path = Req:get(path),
try
    case dispatch(Req, valid_urls:urls()) of
        none -> 
            case filelib:is_file(filename:join([DocRoot, Path])) of
                true -> 
        %% If there's a static file, serve it
                    Req:serve_file(Path, DocRoot);
                false ->
        %% Otherwise the page is not found
                    case Req:get(method) of
                        Method when Method =:= 'GET'; Method =:= 'HEAD' ->
                            case Path of
                                "response" ->
                                    QueryStringData = Req:parse_qs(),

                                    Username = get_username(Req, QueryStringData),
                                    Cookie = make_cookie(Username),

                                    FindCookie = get_cookie_value(Req,"mename","Not Found."),
                                    % render_ok(Req, [Cookie], greeting_dtl, [{username, Username}]),

                                    Req:respond({200, [{"Content-Type", "text/html"}],
                                                "<html><p>Webpage</p></hmtl>"});
                                _ ->
                                    Req:not_found()
                            end
                    end
            end;
        Response -> 
            Response
    end
catch
Type:What ->
        Report = ["web request failed",
                  {path, Path},
                  {type, Type}, {what, What},
                  {trace, erlang:get_stacktrace()}],
        error_logger:error_report(Report),
        %% NOTE: mustache templates need \ because they are not awesome.
        Req:respond({500, [{"Content-Type", "text/plain"}],
                     "request failed, sorry\n"})
end.

我得到的错误是:

[error] "web request failed", path: "response", type: error, what: undef, trace: [{greeting_dtl,render,[[{username,"GET"}]],[]}

错误似乎来自render_ok,但是对Erlang-mochiweb不熟悉我不知道如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

您已注释掉render_ok,但之后未编译该文件。错误消息表明它仍然调用greeting_dtl

我假设您使用的是erlydtl,因为错误位于greeting_dtlrender函数中的错误通常意味着您错误拼写了一些属性名称。检查您的模板在哪里,以及是否有类似{{ something.username }}的内容。将其注释或更改为mename(无论什么都有意义)。