Routing
LiteNode provides a comprehensive routing system covering all HTTP methods, route parameters, wildcards, and router composition.
HTTP Methods
get
get(routePath: string, ...handlers: RouteHandler[]): LiteNode
app.get("/users", (req, res) => {
res.end("List of users")
})
post
post(routePath: string, ...handlers: (RouteHandler | number)[]): LiteNode
Supports application/json, application/x-www-form-urlencoded, and multipart/form-data. The optional trailing number sets the maximum request size in MB (default: 1).
app.post("/users", (req, res) => {
const data = req.body
res.status(201).json({ created: data })
})
See Body Parsing for full details.
put
put(routePath: string, ...handlers: (RouteHandler | number)[]): LiteNode
app.put("/users/:id", (req, res) => {
res.json({ updated: req.params.id })
})
patch
patch(routePath: string, ...handlers: (RouteHandler | number)[]): LiteNode
app.patch("/users/:id", (req, res) => {
res.json({ patched: req.params.id })
})
delete
delete(routePath: string, ...handlers: (RouteHandler | number)[]): LiteNode
app.delete("/users/:id", (req, res) => {
res.json({ deleted: req.params.id })
})
any
Responds to all HTTP methods (GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS).
any(routePath: string, ...handlers: RouteHandler[]): LiteNode
app.any("/health", (req, res) => {
res.json({ status: "ok", method: req.method })
})
redirect
redirect(location: string, statusCode?: number): void
Redirects the client. Status code defaults to 302.
app.get("/old", (req, res) => {
res.redirect("/new", 301)
})
status
status(code: number): nativeRes
Sets the HTTP status code and returns the response object for chaining.
app.get("/created", (req, res) => {
res.status(201).json({ ok: true })
})
Route Parameters
Named Parameters
Use :paramName to capture a segment of the URL.
app.get("/user/:id", (req, res) => {
res.end(`User ID: ${req.params.id}`)
})
// GET /user/123 → "User ID: 123"
Optional Parameters
Append ? to make a parameter optional.
app.get("/users/:id?", (req, res) => {
if (req.params.id) {
res.end(`User: ${req.params.id}`)
} else {
res.end("All users")
}
})
// Matches both /users and /users/42
Query Parameters
Accessed via req.queryParams (a native URLSearchParams instance).
app.get("/search", (req, res) => {
const q = req.queryParams.get("q")
res.end(`Query: ${q}`)
})
// GET /search?q=node → "Query: node"
Wildcard Routes
Single Segment — wildcard()
wildcard(routePath: string, ...handlers: RouteHandler[]): LiteNode
Matches exactly one additional path segment. Equivalent to appending /*.
app.wildcard("/files", (req, res) => {
res.end(`File: ${req.params["*"]}`)
})
// Matches /files/image.png — not /files/dir/image.png
Multi Segment — catchAll()
catchAll(routePath: string, ...handlers: RouteHandler[]): LiteNode
Matches any number of additional path segments. Equivalent to appending /**.
app.catchAll("/api", (req, res) => {
res.json({ path: req.params["**"] })
})
// Matches /api/users, /api/users/123/posts, etc.
Printing the Route Tree
For debugging, print a hierarchical view of all registered routes:
app.printTree()
Example output:
Root
└─ [GET] ↠ (req, res) => { res.end("Hom...
├─ users
└─ [GET] ↠ (req, res) => { res.json({ ...
├─ :id (optional)
└─ [GET] ↠ (req, res) => { const id = ...
├─ files
├─ * (wildcard)
└─ [GET] ↠ (req, res) => { const file...
├─ api
├─ ** (wildcard)
└─ [GET] ↠ (req, res) => { const path...
See the Examples page for more complete routing patterns.