Skip to content

Routing

Nadhi-(Kushi) edited this page Apr 1, 2026 · 2 revisions

Routing

Http-native uses an Express-style API over a native Rust router.

Basic Routes

import { createApp } from "@http-native/core";

const app = createApp();

app.get("/", (req, res) => {
  res.send("Home");
});

app.post("/users", (req, res) => {
  res.status(201).json({ created: true });
});

app.put("/users/:id", (req, res) => {
  res.json({ updated: req.params.id });
});

app.delete("/users/:id", (req, res) => {
  res.sendStatus(204);
});

Params

app.get("/users/:id", (req, res) => {
  res.json({ id: req.params.id });
});

app.get("/posts/:postId/comments/:commentId", (req, res) => {
  res.json(req.params);
});

Query

app.get("/search", (req, res) => {
  res.json({
    q: req.query.q ?? null,
    limit: req.query.limit ?? null,
  });
});

Groups

app.group("/api/v1", (api) => {
  api.get("/users", listUsers);
  api.post("/users", createUser);
  api.get("/users/:id", getUser);
});

Middleware Scope

app.use(async (req, res, next) => {
  await next();
});

app.use("/api", async (req, res, next) => {
  await next();
});

app.all()

app.all(path, handler) registers one handler for all HTTP methods on that exact route path.

app.all("/health", (req, res) => {
  res.json({ method: req.method });
});

Static HTML Routes

app.static() registers an exact GET HTML route:

app.static("/about", "<html><body><h1>About</h1></body></html>");

This is separate from normal route handlers and is meant for the native exact static fast path.

Router Notes

  • exact paths are the cheap path
  • param routes compile with named params
  • app.static() only supports exact paths without params

Clone this wiki locally