Should you use nested routes within NestJS for a "RESTfull" API
Here is an Example API for managing companies, employees, and their children. My entity relationships are as follows: company -1:n-> employees -1:n-> children I’ve structured the API routes like this: GET /companies -> returns all companies GET /company/:id -> returns a single company GET /company/:id/employees -> returns all employees of a single company GET /employee/:id -> returns a single employee GET /employee/:id/children GET /child/:id POST or PUT /company/:id/employees -> creates an employee in a company PATCH /employee/:id ... However, I have a few concerns regarding how to structure these nested routes: The NestJS documentation suggests that using nested routes could lead to maintenance challenges. Are nested routes a bad practice in a RESTful API design for large-scale applications? Hint: This feature should be used very carefully, as overusing it can make code difficult to maintain over time. Should the route GET /company/:id/employees be handled within CompanyController, or would it be better in EmployeeController? What are the pros and cons of each approach? Is it feasible to create a route like GET /company/:id/employee/:id/children, and how should it be structured in terms of controllers in NestJS? If I want to add HATEOAS to my API responses, how can I include links to navigate between entities? Right now, I just return the entities themselves. Are there any libraries or patterns in NestJS that can help with this? Also, for routes like /employee/:id/children, should the link point to /child/:id, or should it be /employee/:id/child/:id? This leads back to my first Question about hard to maintain deeply nested routes. Any guidance or best practices specific to NestJS would be appreciated! Related Questions: Nested Routes in NestJS: https://stackoverflow.com/questions/50438986/how-to-create-nested-routes-with-parameters-using-nestjs Flat vs Nested routes: https://stackoverflow.com/questions/39177431/restful-api-routes-design-nested-vs-non-nested Best Practice for Nested Resources: https://stackoverflow.com/questions/20951419/what-are-best-practices-for-rest-nested-resources
Here is an Example API for managing companies, employees, and their children. My entity relationships are as follows:
company -1:n-> employees -1:n-> children
I’ve structured the API routes like this:
GET /companies -> returns all companies
GET /company/:id -> returns a single company
GET /company/:id/employees -> returns all employees of a single company
GET /employee/:id -> returns a single employee
GET /employee/:id/children
GET /child/:id
POST or PUT /company/:id/employees -> creates an employee in a company
PATCH /employee/:id
...
However, I have a few concerns regarding how to structure these nested routes:
- The NestJS documentation suggests that using nested routes could lead to maintenance challenges. Are nested routes a bad practice in a RESTful API design for large-scale applications?
Hint: This feature should be used very carefully, as overusing it can make code difficult to maintain over time.
Should the route GET /company/:id/employees be handled within CompanyController, or would it be better in EmployeeController? What are the pros and cons of each approach?
Is it feasible to create a route like GET /company/:id/employee/:id/children, and how should it be structured in terms of controllers in NestJS?
If I want to add HATEOAS to my API responses, how can I include links to navigate between entities? Right now, I just return the entities themselves. Are there any libraries or patterns in NestJS that can help with this? Also, for routes like
/employee/:id/children
, should the link point to/child/:id
, or should it be/employee/:id/child/:id
? This leads back to my first Question about hard to maintain deeply nested routes.
Any guidance or best practices specific to NestJS would be appreciated!
Related Questions:
Nested Routes in NestJS: https://stackoverflow.com/questions/50438986/how-to-create-nested-routes-with-parameters-using-nestjs
Flat vs Nested routes: https://stackoverflow.com/questions/39177431/restful-api-routes-design-nested-vs-non-nested
Best Practice for Nested Resources: https://stackoverflow.com/questions/20951419/what-are-best-practices-for-rest-nested-resources