A focused RESTful server framework for Deno 🌰🦕

Overview

acorn

ci deno doc

Rapidly develop and iterate on RESTful APIs using a strongly typed router designed for Deno CLI and Deno Deploy.

import { Router } from "https://deno.land/x/acorn/mod.ts";

const BOOKS: Record<string, { id: number; title: string }> = {
  "1": { id: 1, title: "The Hound of the Baskervilles" },
  "2": { id: 2, title: "It" },
};

const router = new Router();

router.get("/", () => ({ hello: "world" }));
router.get("/books/:id", (ctx) => BOOKS[ctx.params.id]);

router.listen({ port: 5000 });

Routes

An instance of a router has several methods for registering a handler for a route. The methods correspond to one or many HTTP methods or verbs. When a request is handled by the router that matches a route and the HTTP method(s), it will invoke the registered handler.

The handler is provided with a context which contains information about the request:

interface Context<Params extends Record<string, string>, BodyType> {
  readonly request: Request;
  readonly params: Params;
  body(): Promise<BodyType | undefined>;
}

The .params property provides any parameters (named captures) parsed out when matching the route string.

The .body() method is a convenience method to deal with decoding a JSON string body. It can be used with an optional deserializer which can do advanced decoding of the body, or it will attempted to be decoded from the JSON string.

More advanced request body handling can be handled via the .request property.

The handler is then expected to have a return value which can be a Request instance, a value that is a BodyInit, or any other value. If an optional serializer is provided and the response is not a Request instance or of the type BodyInit, the value will be passed to the serializer. If no serializer is present then JSON.stringify() will be used to attempt to convert the value to a JSON string. If undefined is returned, then a 404 NotFound response will be generated.

The handling of the request differs significantly from middleware solutions like oak. In most cases you will want only one handler per route and HTTP method combination. There is nothing that prevents multiple registrations, but the first handler registered that returns a non undefined value will be used and any remaining handlers will not be called.

Underneath, the router matches route strings using the browser standard URL Pattern API and matches the pathname part of the URL.


Copyright 2018-2022 the oak authors. All rights reserved. MIT License.

You might also like...

🛣️ A tiny and fast http request router designed for use with deno and deno deploy

Rutt Rutt is a tiny http router designed for use with deno and deno deploy. It is written in about 200 lines of code and is pretty fast, using an exte

Dec 10, 2022

A small, but powerful HTTP library for Deno & Deno Deploy, built for convenience and simplicity

A small, but powerful HTTP library for Deno & Deno Deploy, built for convenience and simplicity

Wren Wren is a small, but powerful HTTP library for Deno & Deno Deploy, built for convenience and simplicity. convenient aliases for HTTP responses au

Dec 12, 2022

deno-ja (Deno Japanese community) showcase

Showcase Deno本家よりも気軽に作ったものを公開できるようなShowcaseです。 スクリーンショットの撮影方法 短めのidを決めていただいて、下記のようにスクリプトを実行してください。 deno task screenshot [url] [id] ※エラーが出る場合は、下記を実行してみ

Oct 28, 2022

A command-line tool to manage Deno scripts installed via deno install

🏞️ nublar nublar is a command-line tool to manage your scripts installed via deno install. 🛳️ Installation deno install --allow-read --allow-write -

Dec 26, 2022

Lolis-rest - RESTful API for lolis-api

Lolis REST RESTful + Website for Lolis API. Introduction This is a RESTful API which will be used on Lolis API Website and Wrapper. This API uses Imgu

Aug 11, 2022

MiniSense RESTful API

MiniSense RESTful API Why was it developed This project is part of an activity proposed by SenseUp aimed at approving a selection process for a Back-E

Jan 21, 2022

RESTful service to provide API linting as-a-service

API Linting Service Prerequisites / general idea General idea behind this API implementation is to provide an API as a service based on the awesome sp

Mar 14, 2022

🎵 simple and RESTful API for getting lyrics of any song made using Next.js and ChakraUI.

🎵 simple and RESTful API for getting lyrics of any song made using Next.js and ChakraUI.

playground . guide . discord Overview Lyrist is a simple yet powerful RESTful API for getting lyrics of any song using song name and it's artist name.

Dec 17, 2022

Here's a RESTful API that interacts with a PostgreSQL database written in NodeJS with Typescript, RESTify, and Sequelize ORM.

Here's a RESTful API that interacts with a PostgreSQL database written in NodeJS with Typescript, RESTify, and Sequelize ORM.

Basic description REST (Representational State Transfer) is a standard architecture for building and communicating with web services, It typically man

Jan 14, 2022
Owner
oak
A middleware server for Deno 🦕
oak
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
Opinionated collection of TypeScript definitions and utilities for Deno and Deno Deploy. With complete types for Deno/NPM/TS config files, constructed from official JSON schemas.

Schemas Note: You can also import any type from the default module, ./mod.ts deno.json import { type DenoJson } from "https://deno.land/x/[email protected]

deno911 2 Oct 12, 2022
A dockerized uptime monitoring RESTful API server that allows authenticated users to monitor URLs

A dockerized uptime monitoring RESTful API server that allows authenticated users to monitor URLs, and get detailed uptime reports about their availability, average response time, and total uptime/downtime.

Anas Hamed 2 Oct 7, 2022
Uptime monitoring RESTful API server that allows authenticated users to monitor URLs, and get detailed uptime reports about their availability, average response time, and total uptime/downtime.

Uptime Monitoring API Uptime monitoring RESTful API server that allows authenticated users to monitor URLs, and get detailed uptime reports about thei

Mohamed Magdi 2 Jun 14, 2022
RESTful API using Hapi NodeJs Framework. This app is project from Dicoding Couses, Belajar Membuat Aplikasi Back-end untuk Pemula

RESTful API using Hapi NodeJs Framework. This app is project from Dicoding Couses, Belajar Membuat Aplikasi Back-end untuk Pemula

Muhammad Ferdian Iqbal 1 Jan 3, 2022
A server side rendering framework for Deno CLI and Deploy. 🦟 🦕

nat A server side rendering framework for Deno CLI and Deploy. Incorporating acorn, nano-jsx, and twind, it provides the tooling to provide a server c

oak 12 Nov 17, 2022
This is a simple boilerplate for a Deno website, deployed with Deno Deploy.

Simple Deno Website Boilerplate This is a simple website boilerplate built using Deno and deployed using Deno Deploy. Demo at simple-deno-website-boil

Bruno Bernardino 15 Dec 3, 2022
TypeSafe MongoDB Atlas Data API SDK for Deno & Deno Deploy

Atlas SDK atlas_sdk is a TypeSafe MongoDB Atlas Data API SDK for Deno & Deno Deploy Links Docs Import Replace LATEST_VERSION with current latest versi

Erfan Safari 20 Dec 26, 2022
Deno bindings for yoga, using Deno FFI.

deno_yoga Deno bindings for yoga, using Deno FFI. Usage flags: --allow-ffi: Requires ffi access to "yogacore.dll", "libyogacore.so", "libyogacore.dyli

迷渡 6 Feb 11, 2022