我正在尝试构建一个基于Erlang的简单Web服务器,目前为止我可以用下面的代码启动一个服务器。tutorial ref
-module(helloworld).
-export([
main/1,
run_server/0,
start/0,
service/3,
]).
main(_) ->
start(),
receive
stop -> ok
end.
run_server() ->
ok = inets:start(),
{ok, _} = inets:start(httpd, [
{modules, [
mod_alias,
mod_auth,
mod_esi,
mod_actions,
mod_cgi,
mod_dir,
mod_get,
mod_head,
mod_log,
mod_disk_log
]},
{port, 8000},
{server_name,"helloworld"},
{server_root,"/tmp"},
{document_root,"."},
{erl_script_alias, {"/erl", [helloworld]}},
{error_log, "error.log"},
{security_log, "security.log"},
{transfer_log, "transfer.log"},
{mime_types,[
{"html","text/html"}, {"css","text/css"}, {"js","application/x-javascript"} ]}
]).
start() -> run_server().
service(SessionID, _Env, _Input) -> mod_esi:deliver(SessionID, [
"Content-Type: text/html\r\n\r\n", "<html><body>Hello, World!</body></html>" ]).
我正在尝试获取响应,但出现权限错误。
抱歉,您没有权限访问该版块
我的目标是建立一个基于Erlang的服务器,它可以读取帖子请求并将帖子数据存储在MYSQL中。
如果有人可以帮助我的指示或与一些代码启动Erlang服务器读取POST请求,将是非常有帮助的。
谢谢
3条答案
按热度按时间llmtgqce1#
I'm aiming to build Erlang based server which can read post request and store the post data in MYSQL.
I used rebar3 to create an app:
Then I specified mysql-otp as a dependency so that I could use mysql. I basically ignored the OTP app, and I added some source code to start an inets server. See here .
Here's the module I used to start the inets httpd server, handle the request, and insert the post data into a mysql db:
my.erl
:I put that code in the
src
directory of my app, which was namedmyserver
. Here is my directory structure:Here is the
server.conf
file:Then, to run the app I did:
I looked at that output to get the server's port:
55804
, which I used to send a post request with curl:Then I stopped the inets httpd server:
Then I checked the mysql db for a new entry:
Success!
Here's my
rebar.config
file:az31mfrm2#
调用inets:start/0和inets:start/2,在本例中只是其中之一,不知道是否会有区别.
b1payxdu3#
同样写得很差的教程我也遇到了同样的问题。最后他们告诉你去:
但是,URL应为:
(Note删除下划线。)