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

开发的时候先下载好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. 初入Spring-boot(二)

    一.入口类 Spring boot通常有一个名为*Application的入口类,入口类里有一个main方法,这个main方法其实就是一个标准的java应用的入口方法.在main方法中使用Spring ...

  2. spark学习(RDD案例实战)

    练习0(并行化创建RDD) 先启动spark-shell 通过并行化生成rdd scala> val rdd1 = sc.parallelize(List(63,45,89,23,144,777 ...

  3. 自己写的JS排序算法

    这学期刚刚学完数据结构,之前就自己写了一点东西,现在整理一下. <!DOCTYPE html> <html> <head> <meta charset=&qu ...

  4. ubuntu 14.04中安装 ruby on rails 环境(填坑版) 呕血推荐

    环境:在win7 上Vmware虚拟机环境中安装的ubuntu 14.04 开发相关: ruby 2.2.0 rails 4.2.0 sublime text 3 本文说明:所有的命令均在$ 之后,若 ...

  5. 报错HTTP Status 500 - HHH000142: Javassist Enhancement failed: cn.itcast.entity.Customer; nested exception is org.hibernate.HibernateException: HHH000142: Javassist Enhancement failed: cn.itcast.entity.

    报错 type Exception report message HHH000142: Javassist Enhancement failed: cn.itcast.entity.Customer; ...

  6. css属性(冷用法)

    1.pointer-events:none 元素永远不会成为鼠标事件的target. 2.not-allowed  禁止 3.-webkit-appearance: none; appearance ...

  7. Entity Framework 6 Code First 系列:无需修改实体和配置-在MySql中使用和SqlServer一致的并发控制

    无需修改实体和配置,在MySql中使用和SqlServer一致的并发控制.修改RowVersion类型不可取,修改为Timestamp更不可行.Sql Server的RowVersion生成一串唯一的 ...

  8. Django进阶Model篇008 - 使用原生sql

    注意:使用原生sql的方式主要目的是解决一些很复杂的sql不能用ORM的方式写出的问题. 一.extra:结果集修改器-一种提供额外查询参数的机制 二.执行原始sql并返回模型实例 三.直接执行自定义 ...

  9. Android开发调试中遇到的Waiting for HOME解决方案

    显示问题如图: 基本解决思路:在AVD里面单独启动运行一次模拟器,然后尝试运行程序,基本会解决这个问题.

  10. 【lightoj-1094】树的直径(DFS)

    链接:http://www.lightoj.com/volume_showproblem.php?problem=1094 题意: 一共n各节点编号0-n-1, 输入n-1条无向边代表u-v距离为w, ...