Phoenixでサーバーを立ち上げたらエラーが発生したのでその対処のメモ
環境
mac: 10.14
Elixir: 1.7.4
beam: 10.1.1
Phoenix: v1.3.4
1.プロジェクト生成
Phoenixのプロジェクトを mix phx.new **
で生成する時に Fetch and install dependencies? [Yn]
と依存関係のインストールを聞かれる。
この時にYes
にするといろいろとインストールしてくれる。
この後でmix phx.server
としてサーバーを立ち上げるのだが、この時に下記のエラーに遭遇
warning: found quoted keyword "test" but the quotes are not required. Note that keywords are always atoms, even when quoted, and quotes should only be used to introduce keywords with foreign characters in them
mix.exs:57
warning: please add the following dependency to your mix.exs:
{:plug_cowboy, "~> 1.0"}
This dependency is required by Plug.Adapters.Cowboy
which you may be using directly or indirectly.
Note you no longer need to depend on :cowboy directly.
[info] Application react exited: React.Application.start(:normal, []) returned an error: shutdown: failed to start child: ReactWeb.Endpoint
** (EXIT) shutdown: failed to start child: Phoenix.Endpoint.Handler
** (EXIT) "plug_cowboy dependency missing"
** (Mix) Could not start application react: React.Application.start(:normal, []) returned an error: shutdown: failed to start child: ReactWeb.Endpoint
** (EXIT) shutdown: failed to start child: Phoenix.Endpoint.Handler
** (EXIT) "plug_cowboy dependency missing"
plug_cowboy
というモジュールが見つからないというエラー
このエラーが出る前の警告でmix.exs
に追加しろとあるので警告の内容どおり追加
diff --git a/mix.exs b/mix.exs
index aa6ef78..9c842cf 100644
--- a/mix.exs
+++ b/mix.exs
@@ -40,7 +40,8 @@ defmodule React.Mixfile do
{:phoenix_html, "~> 2.10"},
{:phoenix_live_reload, "~> 1.0", only: :dev},
{:gettext, "~> 0.11"},
- {:cowboy, "~> 1.0"}
+ {:cowboy, "~> 1.0"},
+ {:plug_cowboy, "~> 1.0"}
]
end
diffはこんな内容
この状態に編集した後はmix deps.get
で追加したplug_cowboy
をインストールしてやる。
Resolving Hex dependencies...
Dependency resolution completed:
connection 1.0.4
cowboy 1.1.2
cowlib 1.0.2
db_connection 1.1.3
decimal 1.5.0
ecto 2.2.11
file_system 0.2.6
gettext 0.16.0
mime 1.3.0
phoenix 1.3.4
phoenix_ecto 3.6.0
phoenix_html 2.12.0
phoenix_live_reload 1.1.7
phoenix_pubsub 1.1.1
plug 1.7.1
plug_cowboy 1.0.0
plug_crypto 1.0.0
poison 3.1.0
poolboy 1.5.1
postgrex 0.13.5
ranch 1.3.2
* Getting plug_cowboy (Hex package)
Checking package (https://repo.hex.pm/tarballs/plug_cowboy-1.0.0.tar)
Using locally cached package
ここまでやったらmix phx.server
を実行してサーバーが起動する所まで確認
==> plug_cowboy
Compiling 5 files (.ex)
Generated plug_cowboy app
==> react
Compiling 13 files (.ex)
Generated react app
[info] Running ReactWeb.Endpoint with Cowboy using http://0.0.0.0:4000
02:19:21 - info: compiled 6 files into 2 files, copied 3 in 3.2 sec
2.リクエストを送る
これでできたと思い、ブラウザでlocalhost:4000にアクセスしたらまた別のエラー発生
[error] Ranch protocol :error of listener ReactWeb.Endpoint.HTTP (cowboy_protocol) terminated
** (exit) :undef
[error] Ranch protocol :error of listener ReactWeb.Endpoint.HTTP (cowboy_protocol) terminated
** (exit) :undef
ネットで調べてみたがなかなか解決方法が見つからず試しに./_build
ディレクトリを消して再度mix phx.server
を実行してサーバーを立ち上げたら無事リクエストが通りwelcomeページが表示された。