Lesson 26. Adding an API to Your Application

 

In this lesson, you take a first look at reorganizing your routing structure and responding with data. First, you create new folders to house the routes you’ve built in main.js. The new structure follows some of the application programming interface (API) conventions you set up in earlier lessons. Next, you modify some controller actions to respond with Embedded JavaScript (EJS) and JSON, depending on the query parameters. Last, you test your new API connection by creating an Ajax GET request from your client-side JavaScript.

This lesson covers

  • Organizing your routes with namespacing
  • Creating API endpoints to respond with JSON
  • Making Ajax requests from your views
Consider this

Your recipe application renders many pages and offers specific functionality on each page. To make the user experience less complicated, you’d like to allow users to view available programs from their profile pages. To do so, you decide to conditionally serve data in JSON format and display that data through JavaScript and HTML on the client. When you modify your controller actions, your application can offer an API that goes beyond serving web pages.

26.1. Organizing your routes

As your application grows, the routes in your main.js file start to overwhelm other middleware and configurations. Routes are important parts of your application, and keeping your routes organized in a way that multiple developers can manage and understand is arguably as important.

26.2. Creating an API

26.3. Calling your API from the client

Summary