Routing
Learn how to define routes and handle HTTP requests in Kito
Kito provides a simple and intuitive routing system that supports all HTTP methods and dynamic route parameters.
Basic Routes
Define routes using HTTP method helpers:
import { server } from "kitojs";
const app = server();
app.get("/", ({ res }) => res.send("GET request"));
app.post("/data", ({ res }) => res.send("POST request"));
app.put("/update", ({ res }) => res.send("PUT request"));
app.delete("/remove", ({ res }) => res.send("DELETE request"));
app.patch("/modify", ({ res }) => res.send("PATCH request"));Route Parameters
Capture dynamic segments in your URLs:
// Single parameter
app.get("/users/:id", ({ req, res }) => {
const { id } = req.params;
res.json({ userId: id });
});
// Multiple parameters
app.get("/posts/:category/:id", ({ req, res }) => {
const { category, id } = req.params;
res.json({ category, id });
});Parameters are always strings. Use validation to convert them to other types.
Query Parameters
Access query string parameters through ctx.req.query:
// URL: /search?q=kito&limit=10
app.get("/search", ({ req, res }) => {
const { q, limit } = req.query;
res.json({
searchTerm: q,
limit
});
});Request Body
Handle JSON and form data in POST/PUT/PATCH requests:
app.post("/users", ({ req, res }) => {
const body = req.json();
res.json({
message: "User created",
data: body
});
});Route Chaining
Chain multiple routes on the same path using route(). You can also apply middlewares to all routes in the chain:
import { server, router } from "kitojs";
const app = server();
// Basic chaining
app.route("/api/users")
.get(({ res }) => res.json({ users: [] }))
.post(({ res }) => res.json({ created: true }))
.end();
// Chaining with middlewares
app.route("/api/admin", [authMiddleware])
.get(({ res }) => res.send("Admin Dashboard"))
.post(({ res }) => res.send("Admin Action"))
.end();Router
The router() function creates a new router instance that can be mounted to the main server or other routers. This is useful for organizing your application into modules.
import { server, router } from "kitojs";
// Create a router for user routes
const users = router();
users.get("/", ({ res }) => {
res.send("Users list");
});
users.post("/", ({ res }) => {
res.send("Create user");
});
// Mount the router to the main app
const app = server();
app.mount("/users", users);
app.listen(3000);You can also export routers from separate files:
// routes/cats.ts
import { router } from "kitojs";
export default router()
.get("/", ({ res }) => res.send("All cats"))
.post("/", ({ res }) => res.send("New cat"));// index.ts
import { server } from "kitojs";
import catsRoutes from "./routes/cats";
server()
.mount("/cats", catsRoutes)
.listen(3000);Response Methods
Kito provides several methods to send responses:
// Send plain text
app.get("/text", ({ res }) => {
res.send("Hello World");
});
// Send JSON
app.get("/json", ({ res }) => {
res.json({ message: "Hello" });
});
// Send HTML
app.get("/html", ({ res }) => {
res.html("<h1>Hello</h1>");
});
// Set status code
app.get("/not-found", ({ res }) => {
res.status(404).send("Not Found");
});
// Redirect
app.get("/old-page", ({ res }) => {
res.redirect("/new-page");
});Headers and Cookies
Manage response headers and cookies:
app.get("/with-headers", ({ res }) => {
res
.header("X-Custom-Header", "value")
.cookie("session", "abc123", { httpOnly: true })
.json({ success: true });
});File Responses
Send files and downloads:
// Send a file
app.get("/file", ({ res }) => {
res.sendFile("path/to/file.pdf");
});
// Trigger download
app.get("/download", ({ res }) => {
res.download("path/to/file.pdf", "custom-name.pdf");
});All response methods automatically set appropriate Content-Type headers.