@Routes()
and @Route()
decorators. This approach allows you to organize your API endpoints in controller classes with dependency injection support.
Creating a Routes Controller
A routes controller is a class decorated with@Routes()
that contains methods decorated with @Route()
to define individual endpoints:
Route Definition Options
ThecreateRoute()
function accepts the following options:
Available Options
Option | Type | Description |
---|---|---|
path | string | URL path pattern for the route (supports Express path parameters) |
method | 'get' | 'post' | 'put' | 'delete' | 'all' | HTTP method to handle |
bodyParams | Object | Schema | Schema for validating request body |
queryParams | Object | Schema | Schema for validating query parameters |
returns | Object | Schema | Schema for validating response body |
middlewares | Array<RequestHandler> | Express middlewares to apply to this route |
resolve | Function | Function that handles the request and returns a response |
app | express.Application | Optional custom Express app instance |
Route Handlers
Route handlers (theresolve
function) receive the request object and any middleware-injected properties:
Handler Parameters
Route handlers typically receive:req
: Express Request object with params, query, and body- Any context injected by middleware (like viewer, authenticated user)
Return Values
Route handlers should return an object with:Route Parameters
You can access route parameters through the request object, and they will be type-safe if you specify the path with parameter placeholders:Query Parameters
You can define and validate query parameters using thequeryParams
option:
Request Body
You can define and validate request body parameters using thebodyParams
option:
Error Handling
Orionjs automatically handles errors thrown in route handlers:Example: File Uploads
For handling file uploads, you could use themulter
middleware:
CORS and Security
There are two ways to apply middleware in Orionjs:1. Route-Specific Middleware
Apply middleware to specific routes using themiddlewares
option:
2. Global Middleware
Apply middleware to all routes using the HTTP app:Custom Response Types
You can define custom response formats for non-JSON responses:Authentication and Authorization
You can implement authentication and authorization using middleware:Starting the HTTP Server
To start the HTTP server:Best Practices
- Organize by Domain: Group related routes in the same controller class.
- Use Parameter Validation: Define schema for query, body, and response parameters.
- Keep Route Handlers Simple: Focus on request handling logic, move business logic to services.
-
Leverage Dependency Injection: Use
@Inject(() => Service)
for clean service integration. - Implement Proper Error Handling: Catch and handle expected errors appropriately.
- Apply Security Middleware: Use middleware for authentication, CSRF protection, etc.
- Clear Status Codes: Use appropriate HTTP status codes for different responses.
- Document Your API: Add clear descriptions for each endpoint and its parameters.
- Follow RESTful Principles: Use appropriate HTTP methods for different operations.
- Implement Rate Limiting: Protect against abuse with rate limiting middleware.