In this lesson, you change the way that you access JSON-formatted data by adding an API namespace. Then you modify your AJAX function to allow users to join courses directly from a modal. Last, you create the action to link users and courses through a new route.
This lesson covers
- Creating an API namespace
- Building a UI modal to fetch data asynchronously
- Connecting models with MongoDB methods
Consider this
Users can now view course listings from any page on your application, but they want to do more than view that list. With AJAX requests, you can not only pull data asynchronously into the page, but also perform other actions, such as creating new records and editing existing records.
In this lesson, you explore ways in which you can make better use of your API and how AJAX can help.
I discussed namespacing in lesson 26. Now you’re going to implement a namespace for API endpoints that return JSON data or perform actions asynchronously. To get started, create a new route module called apiRoutes.js in your routes folder. This module will contain all the API routes with JSON response bodies. Require this new module in index.js by adding const apiRoutes = require("./apiRoutes"). Then tell your router to use this module under the api namespace with router.use("/api", apiRoutes).