Reference
API Reference
Complete reference for all CanxJS APIs, methods, and utilities.
Core
createApp
createApp(options?: AppOptions): ApplicationCreates a new CanxJS application instance
Example
1import { createApp } from "canxjs";23const app = createApp({4port: 3000,5debug: true,6});
Application.listen
app.listen(callback?: () => void): voidStarts the HTTP server on the configured port
Example
1app.listen(() => {2console.log("Server running on port 3000");3});
Routing
app.get
app.get(path: string, ...handlers: Handler[]): voidRegister a GET route handler
Example
1app.get("/users", (req, res) => {2res.json({ users: [] });3});
app.post
app.post(path: string, ...handlers: Handler[]): voidRegister a POST route handler
Example
1app.post("/users", async (req, res) => {2const user = await User.create(req.body);3res.status(201).json(user);4});
app.put / app.patch
app.put(path: string, ...handlers: Handler[]): voidRegister PUT/PATCH route handlers for updates
Example
1app.put("/users/:id", async (req, res) => {2const user = await User.update(req.params.id, req.body);3res.json(user);4});
app.delete
app.delete(path: string, ...handlers: Handler[]): voidRegister a DELETE route handler
Example
1app.delete("/users/:id", async (req, res) => {2await User.delete(req.params.id);3res.status(204).send();4});
Middleware
app.use
app.use(...middleware: Middleware[]): voidRegister global middleware
Example
1import { logger, cors, json } from "canxjs";23app.use(logger());4app.use(cors());5app.use(json());
logger
logger(options?: LoggerOptions): MiddlewareHTTP request logging middleware
Example
1app.use(logger({2format: "combined",3colorize: true,4}));
cors
cors(options?: CorsOptions): MiddlewareCross-Origin Resource Sharing middleware
Example
1app.use(cors({2origin: ["http://localhost:3000"],3credentials: true,4}));
rateLimit
rateLimit(options?: RateLimitOptions): MiddlewareRate limiting middleware
Example
1app.use(rateLimit({2max: 100,3window: 60000, // 1 minute4}));
Request & Response
req.params
req.params: Record<string, string>Route parameters extracted from the URL
Example
1app.get("/users/:id", (req, res) => {2const userId = req.params.id;3});
req.query
req.query: Record<string, string>Query string parameters
Example
1// GET /users?page=1&limit=102app.get("/users", (req, res) => {3const { page, limit } = req.query;4});
req.body
req.body: anyParsed request body (requires json/urlencoded middleware)
Example
1app.post("/users", (req, res) => {2const { name, email } = req.body;3});
res.json
res.json(data: any): voidSend a JSON response
Example
1res.json({ message: "Success", data: user });
res.status
res.status(code: number): ResponseSet 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): QueryBuilderFilter records by conditions
Example
1const activeUsers = await User2.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({2name: "John Doe",3email: "john@example.com",4});
WebSocket
app.ws
app.ws(path: string, handler: WsHandler): voidRegister a WebSocket route
Example
1app.ws("/chat", (ws, req) => {2ws.on("message", (msg) => {3ws.send(`Echo: ${msg}`);4});5});
broadcast
broadcast(channel: string, data: any): voidBroadcast message to all connected clients
Example
1import { broadcast } from "canxjs";23broadcast("notifications", {4type: "new_message",5data: message,6});