Dealing with multiple REST APIs on a single route using Express - Best practices

As a newcomer to the world of JavaScript programming, I've taken on the challenge of building a rest API for soccer game stats using express JavaScript.

I successfully developed a rest API that manages leagues by creating/showing/updating/deleting them. I created a controller with all these functions and linked each function to a specific route such as www.test.com/soccer/leagues, www.test.com/soccer/league:id, etc. Everything is functioning smoothly.

Now, my goal is to create a rest API that handles teams. Specifically, I want to display all teams participating in a particular league on the league's view at the route like www.test.com/soccer/league:id.

To achieve this, I have set up a new controller with functions for creating/updating/showing all teams/deleting a team and connected this controller to the team routes such as www.test.com/soccer/leagues/team:id.

My query is, how should I manage this? How can I pass the team data to the specific league's route?

Answer №1

If you're looking for insights on demonstrating relationships between collections in REST API design, I suggest checking out this informative article.

When dealing with a "one-to-many" relationship like one league to many teams, there are various ways to query child collections within the API. One approach is creating a "nested" route that exposes a child collection:

www.example.com/soccer/leagues/:id/teams --> Retrieves all teams for a specific league based on provided league ID

This method involves implementing a new route where the id from the route parameter is used to filter the results.

Alternatively, another option is to utilize query parameters to filter results within the entire child collection:

www.example.com/soccer/teams?league_id=123 -> Fetches all teams associated with league_id 123

By adopting this option, you can continue using your existing "teams" endpoint. Simply extract and apply the query parameter to filter results when needed.

It's worth noting that plural names are typically used for endpoints to signify multiple items contained within each collection.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Engaged in revisiting a previous project and stumbling upon unfamiliar code

I need help understanding the code below. It's a bit confusing to me, but I really want to understand it. reverse = [-1, 1][+!!reverse]; ...

Error occurred when attempting to fetch data from a nested JSON object in React/Next.JS

Having trouble retrieving data from my API and encountering this error message. I suspect it may be related to the JSON structure. Interestingly, when using a different URL with nested arrays, it works fine. However, my current data is not cooperating. Any ...

What is the significance of $($(this)) in coding jargon

While browsing the internet, I came across a code snippet that includes the following line: if ($($(this)).hasClass("footer_default")) { $('#abc') .appendTo($(this)) .toolbar({position: "fixed"}); } I'm curious ab ...

Passing a variable between functions in JavaScript: Tips and tricks

let chart; let data = new Array(); function handleClick() { data = document.getElementById('graph:hi').value; alert(data); } let chartData = new Array(66, 15, 2.5, 21.9, 25.2, 23.0, 22.6, 21.2, 19.3, 16.6, 14.8); alert(chartData); jQuery ...

A guide on calling a function from a different component in vuejs2

I am facing a situation where I need to trigger an event whenever my sidebar opens. This event should be called every time the sidebar is opened. For example, when I click on an element, it triggers the opening of my sidebar which then calls a function th ...

JavaScript libraries are not required when using the .append function to add HTML elements

Currently, I am attempting to utilize $.ajax in order to retrieve an html string from a php file and append it to the current html div. Oddly enough, when I use php echo, everything functions properly. However, when I attempt to load dynamically using $.lo ...

Issue arise when utilizing async/await in conjunction with .forEach method

My code is attempting to make a basic request to Redis in order to retrieve all the values (not keys) from my database. However, I am encountering an issue where the tab is being returned before the .forEach function even begins. This is evident because ...

It appears that protractor-flake is programmed to re-run all tests instead of just the ones that have failed

Running tests using the latest version of "[email protected]", encountering failures during testing but the tests are rerunning again. No custom reporter used except Allure reporting. Below is the command used for running: protractor-flake --max-at ...

Inspect the key within a complex hierarchy of nested objects in an array

I am working with an array of nested objects and need to extract the value of a specific key property. Currently, I am using a for loop and checking for the existence of children property, but I feel there might be a more optimal way to accomplish this tas ...

Display or conceal child links using JQuery based on their availability

I have a query regarding JQuery. If I click on Link1, which does not contain any ul.children, the class current_page_item will be added (not shown in this code as it is added automatically by Wordpress). In this scenario, the ul.children in Link2 should be ...

Styling multiple Higher Order Components (HoCs) using Material UI withStyles

When developing my application, I encountered an issue with using Higher Order Components (HoCs) and withStyles for styling. If I apply multiple HoCs to one component, the classes prop of the first HoC gets passed to the next one in the compose chain, caus ...

An error in AngularJS occurred, stating: "TypeError: Attempted to access an undefined property 'apply'"

Having issues with my angularjs app that includes a form with a dropdown list and a datetimepicker. Whenever I change the dropdown list selection, I want the date displayed in the datetimepicker to be updated accordingly. After changing the selected item ...

Upon invoking the useEffect function, the default value can be seamlessly established within the input field of the antd library

I have a function called loadProfile that I need to execute within the useEffect hook. When this function is triggered, I want the name Mario to be automatically displayed in the input field labeled name. How can I set this default value using the antd lib ...

What is the interaction between Handlebars.js, jQuery, and Nodejs Express?

Currently, I am in the process of building a simple website using Express JS and Handlebars. The two main files involved are page1Router.js and page1Router.handlebars. Within the Handlebars file, you will find the following code snippet: <!DOCTYPE html ...

NodeJS Array not populating properly

Currently, I am utilizing mongodb as a database to store valuation data. After retrieving multiple valuation entries from the database, I iterate through each of them to fetch the corresponding offers based on their offer_id. I can confirm that the databas ...

Obtaining a response in string format using the $.ajax function

var module = (function(){ return{ loadData: function(url, success, error){ $.when($.ajax({ type: 'GET', cache: false, url: url, contentType: 'application ...

Issues with $.getjson and trouble with storing JSON data

My current issue involves attempting a getJSON call to a page on my domain that contains a simple JSON array. However, the script in my webpage is not returning anything as expected. <script> $('#genButton').click(function() { ...

Challenges with Loading JSON Dynamically in Next.js using an NPM Package

In my TypeScript project, I have implemented a functionality where a json configuration file is dynamically loaded based on an enum value passed as a parameter to the getInstance function in my PlatformConfigurationFactory file. public static async getIn ...

What is the process for customizing the arrow of a <select> dropdown menu exclusively?

Here is the code for a dropdown menu: <select id="dropdown"> <option value="">Go to page...</option> <option value="http://stackoverflow.com">CSS-Tricks</option> <option valu ...

Is it possible for my commitment to consistently provide identical values, even when the data varies each time it is invoked?

Initially, the getCart method is invoked in b-navbar.component.ts: export class BNavbarComponent implements OnInit{ appUser: any; cart$ : Observable<ShoppingCart | null>; constructor(private auth : AuthService, private shoppingCartService : S ...