Is it more beneficial to categorize REST-based modules/controllers by resource or by specific action/feature?

I'm facing a scenario that goes like this:

Endpoint 1: Retrieve all books in the United States owned by John with a GET Request to /country/us/person/john/books

Endpoint 2: Get all books owned by John in every country with a GET Request to /person/john/books

Endpoint 3: Fetch all books across all countries using a GET Request to /country/books

I am planning on creating a rest server using NestJS (although not directly related to this question) which leverages the concept of modules for organizing controllers.

In this context, should I create:

  • A single module named books containing one controller managing three routes. Or,
  • Split it into two modules, namely booksByCountry and booksByPerson

Is there a best practice or recommendation when approaching this situation?

Answer №1

When setting up your API, the approach you take will depend on how you want to structure it in a meaningful way.
Consider your specific use cases and resource access requirements. For example, if you need to retrieve books by person and/or country, here are a couple of options:

Initial Approach:

API1: Retrieve all books in the US owned by John:
Make a GET Request to

/books/person/{personName}/country/{countryName}

API2: Get all books owned by John in every country:
Access via a GET Request to /books/person/{personName}

API3: Fetch all books across all countries:
Utilize a GET Request to /books

Alternative Perspective:

API1: Retrieve all books in the US owned by John:
Issue a GET Request to

/persons/{personName}/books?country={countryName}

API2: Get all books owned by John in every country:
Access through a GET Request to

/persons/{personName}/books?country=all
(or simply /persons/{personName}/books without specifying all as a query parameter value)

API3: Fetch all books across all countries:
Retrieve using a GET Request to /books

In Summary:

The choice between these approaches hinges on the semantic clarity of your URIs and how you prefer to manage your path and query parameters. Whether your API is intended for public consumption or internal usage only, ensure that the exposure of your resources is straightforward and logical so that users can easily navigate the documentation and utilize the API.

If this explanation has been helpful, feel free to share your thoughts and insights on this topic!

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

Error: The Stripe webhook payload must be submitted as either a string or a Buffer

I'm currently working on setting up a subscription system using Stripe. I have mostly followed the code from their documentation, but I keep encountering the following error: Webhook signature verification failed. Webhook payload must be provided as a ...

Using select2, items can be automatically selected for an ajax call

Is it possible to configure a select2 control to automatically select an item when the ajax response contains extra data? I am looking to set up my controller to mark an item as an exact match in the JsonResult and have the select2 control automatically s ...

Error in Node.js Socket.io: The disconnect event is being triggered before the connect event

When the client reconnects after a network drop, the disconnect event is triggered on the server. Client code: var url ='192.168.1.101', port = '80', socket = io.connect('http://' + url + ':' + port, { &apo ...

Emphasize table cells dynamically

Query How can I dynamically highlight a selected td? Codepen Example View Pen here Code Snippet The map consists of a randomly generated 2D array, like this: map = [[1,1,1,1,0], [1,0,0,0,0], [1,0,1,1,1], [1,0,0,0,1], [1,1, ...

Establishing a connection between the socket and the currently authenticated user through socket.io

My website consists of multiple pages, and when a user logs in, I need to establish a connection between their user ID (stored in MongoDB) and the socket for real-time messaging. Currently, user details are saved in the express session variables upon login ...

What are the most effective methods for utilizing React child components?

I have a particular interest in how to efficiently pass information along. In a different discussion, I learned about the methods of passing specific props to child components and the potential pitfalls of using <MyComponent children={...} />. I am ...

Limitations on Embedding Videos with YouTube Data API

I have been using the Youtube Data API to search for videos, but I am encountering an issue with restricted content appearing in the results. Specifically, music videos from Vevo are showing up even though I only want videos that can be embedded or placed ...

using javascript to trigger android function

Looking to create a button in HTML that triggers a call from an Android device via JavaScript. Here is the code snippet I have tried, but it's not functioning as expected. Please note, I am new to Android development: public class MainActivity extend ...

Unexpected Error with Background Position Variable

Hello, I am attempting to create an animated background effect that moves up and down using the .animate() and .hover() methods in jQuery. Within my DOM, there is a div with id="#menu" containing a UL list where each item has a background positioned at dif ...

Adjusting the inner div dimensions to be 100% of the body's size without relying on the parent div using JavaScript

I have a main layer with multiple layers stacked on top of it. I need the second layer to extend its width to match the body's width. Main Project: http://example.com What I've tried: I looked into: Solution for matching div width with body ...

Understanding the differences between paths and parameters of routes in express.js

My express application has the following routes: // Get category by id innerRouter.get('/:id', categoriesController.getById) // Get all categories along with their subcategories innerRouter.get('/withSubcategories', categoriesControll ...

Switching the background color of alternating divs in reverse order: a step-by-step guide

I am looking to alternate the background color of divs between odd and even, with the last div being grey and the second to last div being green. I have tried using the odd/even classes in CSS, but it did not work as expected. .main{ width:500px; height ...

If the iframe's CSS source is updated, the parent's CSS source will also change

I'm currently working on a unique school project that involves creating multiple CSS styles for different views. <link rel="stylesheet" type="text/css" href="css/main.css" title="main" media="screen"> <link rel="stylesheet" type="text/css" h ...

Can you provide guidance on utilizing the Login Event within the Electron Framework?

I need some assistance in understanding the functionality of the Event: 'login' feature within the Electron Framework. Is this similar to the Password Autofill/Remember Password feature typically found in web browsers? I'm interested in util ...

Is there a way to prevent the Pace js plugin from running on page load, but still have it execute during Ajax requests only?

I have successfully implemented the jquery pace plugin with a progress bar theme. Everything is working well, but I am looking to make it run only on ajax requests. I have tried various solutions found through research, but haven't had any luck. Belo ...

What is the best way to make an AJAX call to an endpoint that redirects from the controller to a different view while passing

In this scenario, I have implemented a search filter for a web application. The filter data is sent to a controller method where the actual filtering process takes place. However, instead of redirecting to the desired result page, the controller redirects ...

Ensuring proper alignment within anchor links and buttons

button, a { height: 30px; display: inline-block; border: 1px solid black; vertical-align: middle; } button > div, a > div { width: 30px; height: 10px; background-color: red; } <button> <div class="buttonDiv"></div> ...

Mozilla Extension: Run code on initial launch

Currently in the process of building an add-on and looking to implement specific code for its initial run. What I aim to achieve is clicking on the add-on button, navigating through files, and selecting an executable file. This browsing action should only ...

Instead of using <div>, try substituting it with a different HTML page

Seeking assistance on an HTML page project that involves jQuery, CSS, and HTML without server-side integration. Encountering an issue when attempting to replace a <div> with content from another HTML file saved on my computer. The main page code sni ...

Unable to locate the value property of a null object

I'm currently working on creating a Shopping Cart using HTML, CSS, JavaScript, and JQuery. The idea is that when you click "Add to Cart" for the orange item, most of the HTML elements will disappear, leaving only the table displaying the Shopping Cart ...