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

开发的时候先下载好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. 快速将对象转化为JSON格式

    1.导入阿里巴巴fastjson包. <!-- fastJson将对象转化为Json对象 --> <dependency> <groupId>com.alibaba ...

  2. Find Min In Rotated Sorted Array2,包含重复数字的反转序列找最小值。

    public int findMin(int[] nums) { return findMin(nums, 0, nums.length - 1); } public int findMin(int[ ...

  3. centos下tomcat自启动

    一.在指定目录创建脚本并赋予755权限 vim /etc/init.d/tomcat #!/bin/bash # # kenny kenny.zhou@tom.com # /etc/rc.d/init ...

  4. jQuery中hover和blur使用代理delegate无效的解决方法

    今天就遇到了这样的小问题: $(document).ready(function(){ $('.status_on').hover(function(){ $(this).html('点击禁用'); ...

  5. yii2:模块

    yii2:模块 模块不同于frontend/frontback单独的前后台单独的主题项目,模块不能单独部署,必须属于某个应用主体(如前后台:frontend/frontback). 模块置于modul ...

  6. Ajax基础(二)--获取服务器文件

    获取服务器文件相关步骤: 1.创建文件: 2.创建XMLHttpRequest对象: 3.获取文件(注意事项:1)在服务器中运行测试:2)注意编码问题,编码要统一). 3.1 获取xml文件: HTM ...

  7. WebService是什么?以及工作原理

    WebService 就是一个应用程序,向外界暴露出公开的API使别人其能在WEB对其进行远程调用,具有跨平台和跨语言的等特点,采用Internet的Http协议进行客户端与服务器之间的交互 由XML ...

  8. 四十一 Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)基本的索引和文档CRUD操作、增、删、改、查

    elasticsearch(搜索引擎)基本的索引和文档CRUD操作 也就是基本的索引和文档.增.删.改.查.操作 注意:以下操作都是在kibana里操作的 elasticsearch(搜索引擎)都是基 ...

  9. IOS-第三方(SDWebImage)

    SDWebImage ReadMe.md 文档 附:SDWebImage框架github下载地址:https://github.com/rs/SDWebImage注1:该文章简单翻译了SDWebIma ...

  10. 神经网络训练时出现nan错误

    现在一直在用TensorFlow训练CNN和LSTM神经网络,但是训练期间遇到了好多坑,现就遇到的各种坑做一下总结 1.问题一;训练CNN的时候出现nan CNN是我最开始接触的网络,我的研究课题就是 ...