在Cowboy(Erlang)中提供静态文件

时间:2017-09-09 23:13:45

标签: erlang cowboy

我阅读了本指南https://ninenines.eu/docs/en/cowboy/2.0/guide/static_files/并实施了

{"/assets/[...]", cowboy_static, {dir, "/var/www/assets"}}

在我的tunnel_app.erl文件中:

-module(tunnel_app).
-behaviour(application).

-export([start/2]).
-export([stop/1]).

start(_Type, _Args) ->
  Dispatch = cowboy_router:compile([
    {'_', [
    {"/info", lobby_handler, []},
    {"/join/:name", join_handler, []},
    {"/player/status/:name", player_status_handler, []},
    {"/tables/info/:table_id", table_info_handler, []},
    {"/tables/play/:table_id/:name/:auth/:action/:x/:y", table_play_handler, []},
    {"/assets/[...]", cowboy_static, {dir, "/static/assets"}}
    ]}]),
  {ok, _} = cowboy:start_clear(my_http_listener, 100,
    [{port, 8080}],
    #{env => #{dispatch => Dispatch}}),
  {ok, MainDoor} = door:go(),
  register(main_door, MainDoor),
  {ok, MainRoom} = room:go(),
  register(main_room, MainRoom),
  tunnel_sup:start_link().

stop(_State) ->
    ok.

我的文件位于static/assets

~/tunnel/ls static
assets
~/tunnel/ls static/assets
actions.js      dashboard.js        hole.js         player.js       tableview.js
board.js        display.js      http.min.js     script.js       tile.js
callback.js     engine.js       info.js         style.css       view.js
client.js       engine_client.js    moves           table.js
clock.js        game.html       notes           tablemaster.js
color.js        ground.js       oboard.js       tableproxy.js
~/tunnel/

当我导航到

http://localhost:8080/assets/game.html

我的浏览器获得了404。

编辑:

项目的根路径是/users/quantum/tunnel/users/quantum/tunnel/src是所有`.erl文件所在的位置。

我尝试将路径更改为"{"/assets/[...]", cowboy_static, {dir, "/users/quantum/tunnel/src/static/assets/"}}"

并导航到localhost:8080/assets/game.html不显示任何内容(甚至不显示404)

啊哈!我在路径中犯了一个错误 - 静态是在项目的根目录中,而不是在src中。

{"/assets/[...]", cowboy_static, {dir, "../static/assets/"}}不起作用,static/assets也不起作用,但是:

{"/assets/[...]", cowboy_static, {dir, "/users/quantum/tunnel/static/assets/"}}确实有用。

0 个答案:

没有答案