C
CanxJS
v1.6.2
  • Learn
  • Blog
  • Showcase
C
CanxJS

Ultra-fast async MVC backend framework for Bun. Build production-ready APIs with elegance and speed.

Resources

  • Documentation
  • Learn
  • Blog
  • Showcase

Documentation

  • Introduction
  • Installation
  • Core Concepts
  • CLI Commands
  • API Reference

Legal

  • Privacy Policy
  • Terms of Service

© 2026 CanxJS. All rights reserved.

Built with ❤️ for Candra Kirana

Back to Learning Center
Beginner
25 min

Working with Controllers

Organize your code with controller decorators.

Working with Controllers

While closures are great for simple apps, Controllers help organize larger applications.

Creating a Controller

Use the CLI to generate a controller:

bash
bun run canx make:controller UserController

Controller Structure

Controllers in CanxJS use decorators to define routes:

typescript
import { Controller, Get, Post } from "canxjs";

@Controller("/users")
export class UserController {
  
  @Get("/")
  async index() {
    return { users: [] };
  }

  @Post("/")
  async create(req) {
    // Create user logic
    return { created: true };
  }
}

Registering Controllers

Register your controller in src/app.ts:

typescript
import { UserController } from "./controllers/UserController";

app.routes((router) => {
  router.controller("/", UserController);
});

Have questions?

Join the discussion on GitHub