Reference

API Reference

Complete reference for all CanxJS APIs, methods, and utilities.

Core

createApp

createApp(options?: AppOptions): Application

Creates a new CanxJS application instance

Example
1import { createApp } from "canxjs";
2
3const app = createApp({
4 port: 3000,
5 debug: true,
6});

Application.listen

app.listen(callback?: () => void): void

Starts the HTTP server on the configured port

Example
1app.listen(() => {
2 console.log("Server running on port 3000");
3});

Routing

app.get

app.get(path: string, ...handlers: Handler[]): void

Register a GET route handler

Example
1app.get("/users", (req, res) => {
2 res.json({ users: [] });
3});

app.post

app.post(path: string, ...handlers: Handler[]): void

Register a POST route handler

Example
1app.post("/users", async (req, res) => {
2 const user = await User.create(req.body);
3 res.status(201).json(user);
4});

app.put / app.patch

app.put(path: string, ...handlers: Handler[]): void

Register PUT/PATCH route handlers for updates

Example
1app.put("/users/:id", async (req, res) => {
2 const user = await User.update(req.params.id, req.body);
3 res.json(user);
4});

app.delete

app.delete(path: string, ...handlers: Handler[]): void

Register a DELETE route handler

Example
1app.delete("/users/:id", async (req, res) => {
2 await User.delete(req.params.id);
3 res.status(204).send();
4});

Middleware

app.use

app.use(...middleware: Middleware[]): void

Register global middleware

Example
1import { logger, cors, json } from "canxjs";
2
3app.use(logger());
4app.use(cors());
5app.use(json());

logger

logger(options?: LoggerOptions): Middleware

HTTP request logging middleware

Example
1app.use(logger({
2 format: "combined",
3 colorize: true,
4}));

cors

cors(options?: CorsOptions): Middleware

Cross-Origin Resource Sharing middleware

Example
1app.use(cors({
2 origin: ["http://localhost:3000"],
3 credentials: true,
4}));

rateLimit

rateLimit(options?: RateLimitOptions): Middleware

Rate limiting middleware

Example
1app.use(rateLimit({
2 max: 100,
3 window: 60000, // 1 minute
4}));

Request & Response

req.params

req.params: Record<string, string>

Route parameters extracted from the URL

Example
1app.get("/users/:id", (req, res) => {
2 const userId = req.params.id;
3});

req.query

req.query: Record<string, string>

Query string parameters

Example
1// GET /users?page=1&limit=10
2app.get("/users", (req, res) => {
3 const { page, limit } = req.query;
4});

req.body

req.body: any

Parsed request body (requires json/urlencoded middleware)

Example
1app.post("/users", (req, res) => {
2 const { name, email } = req.body;
3});

res.json

res.json(data: any): void

Send a JSON response

Example
1res.json({ message: "Success", data: user });

res.status

res.status(code: number): Response

Set the HTTP status code

Example
1res.status(201).json({ created: true });
2res.status(404).json({ error: "Not found" });

Database

Model.all

Model.all(): Promise<T[]>

Get all records from the model

Example
1const users = await User.all();

Model.find

Model.find(id: number | string): Promise<T | null>

Find a record by primary key

Example
1const user = await User.find(1);

Model.where

Model.where(conditions: object): QueryBuilder

Filter records by conditions

Example
1const activeUsers = await User
2 .where({ status: "active" })
3 .orderBy("created_at", "desc")
4 .get();

Model.create

Model.create(data: object): Promise<T>

Create a new record

Example
1const user = await User.create({
2 name: "John Doe",
3 email: "john@example.com",
4});

WebSocket

app.ws

app.ws(path: string, handler: WsHandler): void

Register a WebSocket route

Example
1app.ws("/chat", (ws, req) => {
2 ws.on("message", (msg) => {
3 ws.send(`Echo: ${msg}`);
4 });
5});

broadcast

broadcast(channel: string, data: any): void

Broadcast message to all connected clients

Example
1import { broadcast } from "canxjs";
2
3broadcast("notifications", {
4 type: "new_message",
5 data: message,
6});

Need More Help?

Explore more guides or join our community for support.