大体参考的这里,非常感谢他的例子

开发的时候先下载好cowboy的库,放到~/.erlang里面

code:add_pathz("/Users/mmc/erlang/3rd_libs/cowboy/ebin/").
code:add_pathz("/Users/mmc/erlang/3rd_libs/cowboy/deps/ranch/ebin/").
code:add_pathz("/Users/mmc/erlang/3rd_libs/cowboy/deps/cowlib/ebin/").

ranch和cowlib是rebar get-deps得到的

建立工程

rebar-creator create-app testCowboy

testCowboy_app.erl

-module(testCowboy_app).
-behaviour(application). -export([start/2, stop/1]). -define(C_ACCEPTORS, 100). start(_StartType, _StartArgs) ->
ok = application:start(crypto),
ok = application:start(cowlib),
ok = application:start(ranch),
ok = application:start(cowboy), Routes = route_helper:get_routes(),
Dispatch = cowboy_router:compile(Routes),
Port = get_port(),
TransOpts = [{port, Port}],
ProtoOpts = [{env, [{dispatch, Dispatch}]}],
cowboy:start_http(http, ?C_ACCEPTORS, TransOpts, ProtoOpts). stop(_State) ->
ok. get_port() ->
case os:getenv("PORT") of
false ->
{ok, Port} = application:get_env(http_port),
Port;
Other ->
list_to_integer(Other)
end.

path_helper.erl

-module(path_helper).

-export([get_path/1]).

get_path(ExtraPath)->
{ok,CurrentPath} = file:get_cwd(),
Path = string:concat(CurrentPath,"/"),
string:concat(Path,ExtraPath).

route_helper.erl

-module(route_helper).

-export([get_routes/0]).

get_routes() ->
StaticPath = path_helper:get_path("../static/"),
[
{'_', [
{"/", test_handler, []},
%相对静态文件的路径根据教程没成功,自己弄了个山寨版
{"/static/[...]", cowboy_static, {dir, StaticPath}}
]}
].

test_handler.erl

-module(test_handler).

-export([init/3]).
-export([handle/2]).
-export([terminate/3]). init(_Transport, Req, []) ->
{ok, Req, undefined}. handle(Req, State) ->
{ok, Req2} = cowboy_req:reply(200, [], <<"Hello world!">>, Req),
{ok, Req2, State}. terminate(_Reason, _Req, _State) ->
ok.

testCowboy.app.src

{application, testCowboy,
[
{description, ""},
{vsn, ""},
{registered, []},
{applications, [
kernel,
stdlib
]},
{mod, { testCowboy_app, []}},
{env, [{http_port, }]}
]}.

启动

application:start(testCowboy).

看看加载了哪些application

application:which_applications().

本地访问试试

http://127.0.0.1:8080

cowboy的例子的更多相关文章

  1. cowboy使用restful的例子

    直接贴代码,一切尽在不言中 %% cowboy的restful的文档,一定要好好阅读http://ninenines.eu/docs/en/cowboy/HEAD/manual/cowboy_rest ...

  2. cowboy动态页面的例子

    cowboy的动态页用的是erlydtl,需要先安装erlydtl模板引擎,或者你在dep里面添加 创建工程 rebar-creator create-app testCowboy testCowbo ...

  3. cowboy页面重定向的例子

    创建工程 rebar-creator create-app testCowboy testCowboy_app.erl -module(testCowboy_app). -behaviour(appl ...

  4. cowboy的get和post的例子

    官方get和post的代码是有问题的,1.1下运行crash,这里修改了下,贴代码 创建工程 rebar-creator create-app testCowboy testCowboy_app.er ...

  5. cowboy的cookie和session的例子

    session插件需要下载https://github.com/chvanikoff/cowboy_session 如果session需要分布式存储,可以参考https://github.com/sp ...

  6. [翻译][erlang]cowboy handler模块的使用

    关于Cowboy Cowboy是基于Erlang实现的一个轻量级.快速.模块化的http web服务器. Handlers,用于处理HTTP请求的程序处理模块. Plain HTTP Handlers ...

  7. [翻译][erlang]cowboy路由模块使用

    Cowboy是基于Erlang实现的一个轻量级.快速.模块化的http web服务器. 本文官方原文:http://ninenines.eu/docs/en/cowboy/1.0/guide/rout ...

  8. Erlang cowboy 处理不规范的client

    Erlang cowboy 处理不规范的client Cowboy 1.0 參考 本章: Dealing with broken clients 存在很多HTTP协议的实现版本号. 很多广泛使用的cl ...

  9. Erlang cowboy routing 路由

    Erlang cowboy routing 路由 本文译自: http://ninenines.eu/docs/en/cowboy/1.0/guide/routing/ Routing 默认情况下,C ...

随机推荐

  1. springBean集合注入的方法

    applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xm ...

  2. scala学习手记32 - trait选择性混入

    继续上一节. 狗当然是人类的好朋友.但是藏獒呢?这玩意儿又蠢又笨又凶狠,肯定不能算很多人的好朋友了.其实,刚才那句话还可以修正一下下:我们接受的狗才是我们的好朋友. 用程序怎么实现呢?在java里面, ...

  3. 智课雅思词汇---二十三、动词性后缀-ate-fy-ish-ize

    智课雅思词汇---二十三.动词性后缀-ate-fy-ish-ize 一.总结 一句话总结: 1.-ate(differentiate,maturate)? 后缀:-ate ①[动词后缀] 表示做.造成 ...

  4. Button之常用事件

    Button之常用事件 一.简介 1.button介绍 本文介绍了Buttonn的点击事件,触摸事件,获得焦点事件 接口分别为:OnClickListener,OnTouchListener,OnFo ...

  5. shell 数组操作

    1. 定义数组: var_array=(one two three four five) 2.常用操作 获取数组长度: ${#var_array[@]} 获取所有数组元素:  ${var_array[ ...

  6. sass基础篇

    scss的语法非常简单: $color: red; div{ color: $color; } 这就是一个简单的scss代码. 但是,使用 Sass 进行开发,那么是不是直接通过“<link&g ...

  7. hibernate映射(一对一、一对多、多对一、多对多)配置 【转】

    多对一关联映射:在多的一端加入一个外键指向一的一端,它维护的关系是多指向一 一对多关联映射:在多的一端加入一个外键指向一的一端,它维护的关系是一指向多 也就是说一对多和多对一的映射策略是一样的,只是站 ...

  8. Week03-Java学习笔记第三次作业

    Week03-面向对象入门 1.本周学习总结 初学面向对象,会学习到很多碎片化的概念与知识.尝试学会使用思维导图将这些碎片化的概念.知识点组织起来.请使用工具画出本周学习到的知识点及知识点之间的联系. ...

  9. 2017.11.2 Talk to customers for an hour

    yesterday::: Hi Huang, For the better performance of the test the Con 6 should be connected all the ...

  10. 《Drools7.0.0.Final规则引擎教程》第2章 追溯Drools5的使用

    2.1 Drools5简述 上面已经提到Drools是通过规则编译.规则收集和规则的执行来实现具体功能的.Drools5提供了以下主要实现API: KnowledgeBuilder Knowledge ...