wktdev
Can someone please help me with this (Basic API creation) Rubber Ducky Edition
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.
Most Liked
jhefreyzz
You put resources "/items", ItemController, except: [:new, :edit] inside a scope block as mentioned above.
In your case it should be like this:
pipeline :api do
plug :accepts, ["json"]
end
scope "/", AppWeb do
pipe_through :api
resources "/items", ItemController, except: [:new, :edit]
end
To have an idea of what routes being generated of the resources then type mix phx.routes in your terminal.
wktdev
Thanks this worked. I appreciate it. The prompt instructions in the command line should be more clear and beginner friendly. The documentation is difficult.
Add the resource to your :api scope in lib/app_web/router.ex:
resources "/items", ItemController, except: [:new, :edit]
I assumed they wanted me to put the code in the function with the :api atom.
They should have an example.
EDIT:
The documentation does have an example but I was reading off the command prompt messages. So for anyone reading this, the command prompt can be confusing.
scope "/api", LinksWeb do
pipe_through :api
resources "/urls", UrlController, except: [:new, :edit]
end
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









