嘿,我正在尝试让我正在制作的网站向用户发送一个 cookie,然后显示一个网页。
所以我发现http://alexmarandon.com/articles/mochiweb_tutorial/作为关于如何制作 cookie 的唯一真正教程,但它似乎对我来说是错误的。
我的循环看起来像这样(我的 make_cookie、get_cookie_value、render_ok 和 get_username 与他的相同,只是我使用 'mename' 作为键而不是 'username'):
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来说是新手,我不确定如何解决这个问题。