Learn the fundamental building blocks of CanxJS applications. Master these concepts to build powerful, scalable APIs.
Organize your application structure with smart, auto-scanning modules.
Modules organize components, controllers, and services into cohesive blocks.
@Module({
controllers: [UserController],
providers: [UserService],
exports: [UserService]
})
export class UserModule {}Automatically discover and register modules without manual imports using Bun.Glob.
// In your main entry file
import { Module } from 'canxjs';
// Automatically finds and registers all modules in ./src
await Module.scan('./src');Define and manage application routes with a powerful, intuitive API.
Define routes using HTTP method helpers like app.get(), app.post(), app.put(), and app.delete().
app.get("/users", (req, res) => {
res.json({ users: [] });
});
app.post("/users", (req, res) => {
const user = req.body;
res.json({ created: user });
});Capture dynamic values from URLs using colon syntax.
app.get("/users/:id", (req, res) => {
const { id } = req.params;
res.json({ userId: id });
});Organize routes with prefixes and shared middleware.
app.group("/api/v1", (router) => {
router.get("/users", getUsers);
router.get("/posts", getPosts);
});Organize request handling logic into reusable controller classes.
Create controller classes to group related route handlers.
export class UserController {
async index(req: Request, res: Response) {
const users = await User.all();
res.json(users);
}
async show(req: Request, res: Response) {
const user = await User.find(req.params.id);
res.json(user);
}
}Generate CRUD operations automatically with resource controllers.
// Registers: index, show, store, update, destroy
app.resource("/posts", PostController);Intercept and modify requests before they reach your route handlers.
Middleware functions receive request, response, and a next function.
const authMiddleware = async (req, res, next) => {
const token = req.headers.authorization;
if (!token) {
return res.status(401).json({ error: "Unauthorized" });
}
req.user = await verifyToken(token);
next();
};Apply middleware globally, to route groups, or individual routes.
// Global middleware
app.use(logger());
app.use(cors());
// Route-specific
app.get("/admin", authMiddleware, adminHandler);
// Group middleware
app.group("/api", { middleware: [authMiddleware] }, (router) => {
router.get("/profile", profileHandler);
});Query and manage your database with an elegant ORM.
Create model classes that map to database tables.
import { Model } from "canxjs/orm";
export class User extends Model {
static table = "users";
id!: number;
name!: string;
email!: string;
createdAt!: Date;
}Use the fluent query builder for database operations.
// Find all users
const users = await User.all();
// Find by ID
const user = await User.find(1);
// Query with conditions
const admins = await User.where("role", "admin")
.orderBy("createdAt", "desc")
.limit(10)
.get();Build server-rendered views using familiar JSX syntax.
Create views using JSX for type-safe, component-based templates.
// views/Home.tsx
export function Home({ title, users }) {
return (
<Layout title={title}>
<h1>Welcome to {title}</h1>
<ul>
{users.map(user => (
<li key={user.id}>{user.name}</li>
))}
</ul>
</Layout>
);
}Render views from your controllers with data.
app.get("/", async (req, res) => {
const users = await User.all();
res.render(Home, { title: "My App", users });
});