It’s been a day with Plug, I have experienced some problems probably raising from my simple mistakes.
I have two routers, the main router is:
defmodule Startup.Router do
use Plug.Router
plug Plug.Parsers, parsers: [Plug.Parsers.URLENCODED, Plug.Parsers.JSON], json_decoder: Poison
plug :match
plug :dispatch
alias Startup.QuestionRouter
forward "/questions", to: QuestionRouter
match _ do
send_resp(conn, 404, "Path specified incorrectly.")
end
end
And the second router is:
defmodule Startup.QuestionRouter do
use Plug.Router
import Ecto.Query
alias Startup.Question
alias Startup.Repo
if Mix.env == :dev do
use Plug.Debugger
plug Plug.Logger, log: :debug
end
plug :match
plug :dispatch
get "/" do
query = (from q in Question, select: q)
questions = query |> Repo.all
|> Poison.encode!
conn
|> put_resp_content_type("application/json")
|> send_resp(200, questions)
end
end
However GET request to path “/questions” raises error:
(FunctionClauseError) no function clause matching in Startup.QuestionRouter.do_match/4
It’s been 4 hours I’m trying to fix the problem but I couldn’t. Documentation clearly states:
forward "/users", to: UserRouter
Assuming the above code, a request to /users/sign_in will be forwarded to the UserRouter plug, which will receive what it will see as a request to /sign_in.


















