Skip to content
2.0 alphaSee what changed

Express, production-ready by default

Typed route trees, validation from any schema library and production defaults, on the Express you already know.

npm create kaelum@next my-app
import kaelum, { k } from "kaelum";
const app = kaelum();
app.route("/users", {
get: () => db.users.all(),
post: {
body: k.object({ name: k.string().min(2) }),
handler: (c) => c.created(db.users.create(c.body)),
},
"/:id": {
use: [auth],
get: (c) => db.users.find(c.params.id) ?? c.notFound(),
},
});
await app.start(3000);
app.route("/orgs/:orgId", {
"/users/:userId": {
patch: {
body: k.object({ role: k.enum(["admin", "user"]) }),
handler: (c) => {
c.params; // { orgId: string; userId: string }
c.body; // { role: "admin" | "user" }
// @ts-expect-error: "email" is not in the body schema
c.body.email;
},
},
},
});
// POST /users { "name": "A" }
HTTP/1.1 400 Bad Request
X-Request-Id: 5f0c3c1e-8a2b-4d7e-9c1a-2b6f0e4d8a11
{
"error": {
"code": "VALIDATION_ERROR",
"message": "Validation failed",
"details": {
"issues": [
{ "source": "body", "path": "name",
"message": "Must be at least 2 characters" }
]
}
}
}
import { testApp } from "kaelum/testing";
const client = testApp(createApp());
test("creates a user", async () => {
const res = await client.post("/users", { name: "Ada" });
assert.equal(res.status, 201);
assert.equal(res.body.name, "Ada");
});
Why Kaelum

The setup you would write anyway, done

Everything an Express app needs before it is safe to deploy, on from the first line.

Route trees

Your API, shaped like your API

Nest paths, share middleware with use, and see exactly which routes you get. Hover a line to follow it.

app.route("/users", {
"/:id": {
use: [auth],runs for everything below
"/posts": {
},
},
});
Less code

Same endpoint, a fraction of the code

A validated POST /users with security headers, a body limit, request IDs, a health check, JSON errors and graceful shutdown. In Kaelum all of that is the default.

Express 34 lines

server.js
import express from "express";
import helmet from "helmet";
import { randomUUID } from "node:crypto";
import { z } from "zod";
const app = express();
app.disable("x-powered-by");
app.use(helmet());
app.use(express.json({ limit: "100kb" }));
app.use((req, res, next) => {
res.setHeader("X-Request-Id", randomUUID());
next();
});
const NewUser = z.object({ name: z.string().min(2) });
app.post("/users", async (req, res, next) => {
try {
const parsed = NewUser.safeParse(req.body);
if (!parsed.success) {
return res.status(400).json({ error: parsed.error.issues });
}
res.status(201).json(await db.users.create(parsed.data));
} catch (err) {
next(err);
}
});
app.get("/health", (req, res) => res.json({ status: "ok" }));
app.use((req, res) => res.status(404).json({ error: "Not found" }));
app.use((err, req, res, next) => res.status(500).json({ error: "Internal error" }));
const server = app.listen(3000);
process.on("SIGTERM", () => server.close(() => process.exit(0)));

Kaelum 10 lines

server.ts
import kaelum, { k } from "kaelum";
const app = kaelum();
app.post("/users", {
body: k.object({ name: k.string().min(2) }),
handler: (c) => c.created(db.users.create(c.body)),
});
await app.start(3000);

Start a project in one command

JavaScript or TypeScript, API or web, with tests included.