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-appimport 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 RequestX-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.
Secure by defaultSecurity headers, a body size limit and errors that never leak. CORS, rate limiting, CSRF and IP rules are one option away.Learn more Route treesDeclare a whole API as one nested object. Middleware in
use applies to its level and everything below.Learn more Types without genericsPath params, bodies and queries are inferred from the path and the schema. Write the route, get the types.Learn more Any schema libraryZod, Valibot, ArkType or the built-in k. Invalid input answers 400 with every issue listed.Learn more ObservableRequest IDs, request logs, a structured logger, Server-Timing and a health check, all wired up.Learn more Still ExpressEvery Express middleware works. Mount a Kaelum app inside an existing Express app and migrate one route at a time.Learn more 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
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
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.