✨ Basics
Routes

Routes in AdonisJS

Routes in AdonisJS

Routes in AdonisJS define endpoints and specify how HTTP requests should be handled by controllers or handlers. They play a crucial role in mapping incoming requests to corresponding actions in your application.

Defining Routes

Routes are defined in start/routes.ts or a similar file where you can specify endpoints, HTTP methods, controller actions, middleware, and route naming.

Example Routes Definition

Here's an example of defining routes using the AdonisJS router, mapping endpoints to controller actions and applying middleware.

start/routes.ts
import router from "@adonisjs/core/services/router";
import AccountController from "./controllers/account_controller";
import BillingController from "./controllers/billing_controller";
import { middleware } from "#start/kernel";
 
// Account Routes
router.group(() => {
  router.get("/", [AccountController, "update"]).as("account.update");
}).prefix("/account").use([middleware.auth()]);
 
// Billing Routes
router.group(() => {
  router.get("/", [BillingController, "update"]).as("account.billing.update");
}).prefix("/account/billing").use([middleware.auth()]);
 
export default router;

Router Groups

Router groups allow you to organize routes with common properties such as prefix and middleware.

Example Account Routes

router.group(() => {
  router.get("/", [AccountController, "update"]).as("account.update");
}).prefix("/account").use([middleware.auth()]);
  • Grouping: Routes related to account operations are grouped under /account.
  • Middleware: The auth middleware is applied to enforce authentication for all routes within the group.
  • Controller Action: The update method from AccountController is mapped to handle GET requests to /account.

Example Billing Routes

router.group(() => {
  router.get("/", [BillingController, "update"]).as("account.billing.update");
}).prefix("/account/billing").use([middleware.auth()]);
  • Grouping: Routes related to billing operations are grouped under /account/billing.
  • Middleware: The auth middleware is applied similarly to enforce authentication.
  • Controller Action: The update method from BillingController is mapped to handle GET requests to /account/billing.

Middleware

Middleware functions can be applied globally or to specific routes or route groups to perform tasks such as authentication, logging, or request manipulation.

Middleware Example

router.group(() => {
  // Routes and middleware definitions here
}).use([middleware.auth()]);
  • Auth Middleware: Ensures that routes within the group require authentication before being accessed.

Controller Actions

Controller actions define the logic that handles HTTP requests and generates HTTP responses.

Example Controller Action

router.get("/", [AccountController, "update"]).as("account.update");
  • Controller: Specifies AccountController as the handler for the route.
  • Action: Maps the update method in AccountController to handle GET requests to /account.

Route Naming

Route names (as("account.update")) provide a convenient way to refer to routes in your application, facilitating route generation and URL generation.

Registering route file

You can register your route file in the adnoisrc.ts file. Inside the preloads property, add your route file.

adonisrc.ts
  preloads: [
    () => import("#stripe/stripe_router"),
    () => import("#auth/auth_router"),
    () => import("#dashboard/dashboard_router"),
    () => import("#account/account_router"),
    () => import("#product/product_router"),
    () => import("#waitlist/waitlist_router"),
    () => import("#common/common_router"),
    () => import("#start/kernel"),
    () => import('#openai/openai_router'),
  ],

See more

Defining routes in AdonisJS allows you to structure your application's API endpoints, connect them to controllers for business logic, and apply middleware for security and request processing.

Tips! You can generate routes & controllers with already everything imported with the node ace make:module module_name command.