Installation
Create a Controller
Create your first controller with tsoa decorators:
src/controllers/userController.ts
Generate Routes and Spec
Add scripts to your Generate routes and OpenAPI spec:This creates:
package.json:package.json
src/routes.ts- Generated route handlersbuild/swagger.json- OpenAPI specification
Koa Context Access
Access the Koa context object in your controllers:Middleware Integration
Apply Koa middleware to specific routes or controllers:Error Handling
Koa uses a different error handling pattern than Express:Using ctx.throw
Koa provides a convenientctx.throw() method:
State Management
Share data between middleware usingctx.state:
Base Path Configuration
Configure a base path for all routes:tsoa.json
/api/v1.
Router Prefix
Alternatively, use Koa Router’s prefix option:Query Parameters
Headers
Comparison with Express
- Express
- Koa
Next Steps
File Uploads
Learn how to handle file uploads with Koa
Authentication
Secure your API with authentication
Validation
Implement request validation
Dependency Injection
Use IoC containers with your controllers