I am using Phoenix1.17 and the respective versions of Elixir and Erlang. I used ASDF to set it up on Ubuntu
I created a basic Phoenix Instance
mix phx.new app
I install all the dependencies. When the installer gets to the page asking me to run mix ecto.create
I stop what I am doing.
At his point I create a new command line instance, go into PostGres and I create a database. I name the database app.
I verify the existence of the database in Postgres using the command: \l
I go back to to the command line instance that I am running the phoenix install from. I CD into the app directory. I run mix ecto.create
I get a validation message
Compiling 15 files (.ex)
Generated app app
The database for App.Repo has been created
I go to the phoenix documentation to look up how to generate routes. The Phoenix documentation doesn’t say the right way to do it directly; instead, it demonstrates it in the context of building an app. I don’t want to build the app in the documentation, I simply want to know the correct syntax and read a few assuring words explaining it.
Based on what they have this is the synax they use:
mix phx.gen.json Urls Url urls link:string title:string
I am going to replace Urls Url urls with Items Item items and I am only going to have one table called name:string
mix phx.gen.json Items Item items name:string
I get the prompt
Add the resource to your :api scope in lib/app_web/router.ex:
resources "/items", ItemController, except: [:new, :edit]
I do what it says. I go to router and put that puppy in there:
pipeline :api do
plug :accepts, ["json"]
resources "/items", ItemController, except: [:new, :edit]
end
I run the following command:
mix ecto.migrate
Welp, I have a warning. I have no idea what it means or why it appears.
Compiling 7 files (.ex)
warning: no route path for AppWeb.Router matches "/api/items/#{item}"
lib/app_web/controllers/item_controller.ex:18: AppWeb.ItemController.create/2
warning: ItemController.init/1 is undefined (module ItemController is not available or is yet to be defined)
lib/app_web/router.ex:2: AppWeb.Router.__checks__/0
Generated app app
21:16:43.543 [info] == Running 20230205031130 App.Repo.Migrations.CreateItems.change/0 forward
21:16:43.547 [info] create table items
Can someone explain how the hell I’m supposed to read this? There is no hint as to how to fix this
Ignoring the warning, the next thing I want to do is determine if my app configured correctly. I honestly have no idea how to do that other than to run it and assume that if I go to 4000/items that I will see an empty array or JSON object.
mix phx.server
I go to localhost:4000/items
A missing route.
I try localhost:4000/api/items
A missing route.
I looked at the Phoenix instructions and it says to run a curl command. I did.
curl -i http://localhost:4000/api/items
I got this:
HTTP/1.1 404 Not Found
cache-control: max-age=0, private, must-revalidate
content-length: 56354
content-type: text/html; charset=utf-8
date: Sun, 05 Feb 2023 03:24:09 GMT
server: Cowboy
What am I doing wrong?
EDIT
I thought I shut down the server and ran it again. The error below ran.
(Mix) Could not start application app: App.Application.start(:normal, []) returned an error: shutdown: failed to start child: AppWeb.Endpoint
** (EXIT) shutdown: failed to start child: {:ranch_listener_sup, AppWeb.Endpoint.HTTP}
** (EXIT) shutdown: failed to start child: :ranch_acceptors_sup
** (EXIT) {:listen_error, AppWeb.Endpoint.HTTP, :eaddrinuse}
The error has no hint that another instance of the server is running. Shouldn’t it tell the operator that “You may have another instance running on this port”. Most other platforms will tell you that.