Now that we have a proper API, we can remove the fake-api
created earlier and make proper HTTP requests. We'll also look at how the Nrwl Express generators created a helpful proxy configuration for us.
- Learn how to connect frontend and backend apps in an Nx workspace
- We can now delete the
fake-api
from thegame-store
app
-
Use
fetch
in auseEffect
hook in the app.tsx component and call your new API as an HTTP request. We also added a local state to track changes.⚠️ Notice how we assume it will be available at/api
(more on that below)
-
Run
nx serve api
⚠️ Notice the PORT number
-
In a different tab, run
nx serve store
⚠️ Again, notice the PORT number
-
Everything should still look/function the same!
🎓 You can inspect your Network tab in the dev tools and notice an XHR request made to
http://localhost:4200/api/games
🎓 Even though the frontend and server are being exposed at different ports, we can call /api
from the frontend store because Nx
created a proxy configuration for us (see apps/store/proxy.conf.json
) so any calls to /api
are being routed to the correct address/port where the API is running.
This helps you avoid CORS issues while developing locally.
Now let's load the full game in our routed component!
-
Inside the
libs/store/feature-game-detail/src/lib/game-detail
folder, replace the following files:⚠️ Notice how we're using the sharedformatRating()
function in our routed component as well!
- Your component should look similar to the provided screenshot! (you might need to restart your
nx serve store
so the new button styles can be copied over)
- Inspect what changed from the last time you committed, then commit your changes
🎓 If you get stuck, check out the solution