acorn is a RESTful server framework for handling HTTP requests across Deno, Node.js, Bun and Cloudflare Workers.
deno add jsr:@oak/acorn
npx jsr add @oak/acorn
yarn dlx jsr add @oak/acorn
pnpm dlx jsr add @oak/acorn
bunx jsr add @oak/acorn
import { Router } from "@oak/acorn";
const BOOKS = {
"1": {
title: "The Hound of the Baskervilles",
author: "Doyle, Arthur Conan",
},
"2": {
title: "It",
author: "King, Stephen",
},
};
const router = new Router();
router.get("/", () => ({ hello: "world" }));
router.get("/books/:id", (ctx) => BOOKS[ctx.params.id]);
router.listen({ port: 3000 });
acorn focuses on the primary use case of handling JSON data in a RESTful way, with a focused and familiar router API.
See the docsacorn includes a schema validation system via Valibot for requests and responses, making it easy to ensure your API is consistent.
See the docsimport { Router, Status, v } from "@oak/acorn";
const router = new Router();
const db = await Deno.openKv();
const book = v.object({
author: v.string(),
title: v.string(),
});
router.get("/book/:id", async (ctx) => {
const maybeBook = await db.get(["books", ctx.params.id]);
if (!maybeBook) {
ctx.throw(Status.NotFound, "Book not found");
}
return maybeBook.value;
}, { schema: { response: book } });
router.put("/book/:id", async (ctx) => {
const body = await ctx.body();
await db.set(["books", ctx.params.id], body);
return book;
}, { schema: { body: book, response: book } });
router.listen({ port: 3000 });