Simple App

A complete REST API built with Katal demonstrating the most common patterns: authentication, CORS, rate limiting, validation, and route groups.

Source: examples/simple/app.ts

What This Covers

Feature Where
JWT auth setup Auth + createAuthMiddleware
Global CORS createCorsMiddleware
Global rate limiting createRateLimitMiddleware
Protected + public routes named middleware on route groups
Request validation route-level validation schema
Login endpoint password hash + generateToken
CRUD controllers Controller subclasses

Run

cd examples/simple
bun run app.ts

Server starts on http://localhost:3000.

Endpoints

Method Path Auth Description
GET /health health check
POST /auth/login issue JWT
GET /users required list users
GET /users/:id required get user
POST /users required create user
PUT /users/:id required update user
DELETE /users/:id required delete user

Key Patterns

// Global middleware
app.use(createCorsMiddleware({ origin: "*", credentials: true }));
app.use(createRateLimitMiddleware({ windowMs: 60000, maxRequests: 100 }));

// Protected group
router.group("/users", (r) => {
  r.get("/", GetUsersController, { middleware: ["auth"] });
  r.get("/:id", GetUserController, { middleware: ["auth"] });
  r.post("/", CreateUserController, {
    middleware: ["auth"],
    validation: {
      name: { required: true, type: "string" },
      email: { required: true, type: "email" },
    },
  });
});