Routing in Cortado

Learn how Cortado handles file-based routing, request flows, and route-specific logic with built-in flexibility.

Architecture

Middleware

How Routing Works

Cortado uses a file-based routing system where every file inside the routes/ directory automatically becomes an endpoint. The folder structure maps directly to the URL path, making it intuitive and scalable. This means a file at routes/user/profile.ts becomes /user/profile in your API — no additional setup required.

File-to-URL Mapping

This routing system eliminates the need for manual route registration. Instead, you organize routes through folders and filenames, keeping things simple and transparent. Dynamic routes can be defined using bracket notation (e.g. [id].ts), allowing you to capture variables from the URL.

Some core patterns include:

  • routes/index.ts/

  • routes/blog.ts/blog

  • routes/user/[id].ts/user/:id

  • routes/api/[...slug].ts → wildcard matching

This approach allows developers to focus on logic, not wiring.

Middleware and Flow Control

Cortado supports middleware at multiple levels. You can attach global middleware (e.g. for logging or auth) or use per-route middleware to handle validation, permissions, or formatting. Middleware functions receive the request and response objects and can choose to continue or terminate the chain.

Middleware is just a function — but in Cortado, it’s also composable and context-aware.

This makes it easy to apply shared logic across routes, without duplicating code or introducing complexity.

Route Methods and Handlers

Each route can export specific HTTP method handlers like get, post, or delete. These are just named exports inside the route file. If you need multiple methods on the same path, you can define them all in one file, keeping logic tightly scoped and easy to read.

You can also split logic into reusable functions or import shared helpers, enabling DRY (don’t repeat yourself) patterns across your project.

Securing Routes

Thanks to Cortado's modular structure, you can secure routes with per-endpoint authentication and authorization. Middleware can extract tokens, verify sessions, or check user roles. This gives you full control over access without sacrificing speed or readability.

Common use cases:

  • Require login for protected endpoints

  • Restrict admin-only actions

  • Add rate limiting or custom headers

Routing That Grows With You

As your project grows, Cortado’s routing system scales naturally. You can nest folders, group API versions, or separate internal vs. public routes — all within the same folder structure. Combined with strong middleware support, this gives you the foundation for both small and enterprise-grade APIs.

Whether you're building a microservice or a full backend, Cortado's routing gives you the flexibility to scale cleanly and confidently.

Was this helpful?

Dismiss

Was this helpful?

Dismiss

Last updated on

Aug 2, 2025