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.