logo

NHttp

An Simple Http Route framework for Deno and Friends.

ci coverage codefactor denoland deps size licence prs nestland


Features

  • Simple Performance.
  • Middleware support.
  • Sub Router support.
  • Return directly on handlers.
  • Includes body parser with verify body.
  • Cross Runtime support (Deno, Bun, Node, etc).

See examples

Installation

deno.land

import { nhttp } from "https://deno.land/x/nhttp@1.1.13/mod.ts";

deno-npm

import { nhttp } from "npm:nhttp-land@1.1.13";

nest.land

import { nhttp } from "https://x.nest.land/nhttp@1.1.13/mod.ts";

npm/yarn

npm i nhttp-land

// or

yarn add nhttp-land
import { nhttp } from "nhttp-land";

Usage

import { nhttp } from "https://deno.land/x/nhttp@1.1.13/mod.ts";

const app = nhttp();

app.get("/", (rev) => {
  rev.send("Hello, World");
});

// return directly
app.get("/cat", () => {
  return "Hello, Cat";
  // or json
  // return { name: "cat" };
});

app.listen(8000, () => {
  console.log("> Running on port 8000");
});

Run

deno run -A myapp.ts

Deno Flash

requires --unstable flag.

const app = nhttp({ flash: true });

Middleware

const app = nhttp();

app.use((rev, next) => {
  rev.foo = "bar";
  return next();
});

app.get("/", ({ foo }) => foo);

Body Parser

Support json / urlencoded / multipart / text.

note: nhttp automatically parses the body.

const app = nhttp();

// if want disable bodyParser
// const app = nhttp({ bodyParser: false });

app.post("/save", (rev) => {
  console.log(rev.body);
  return "success save";
});

// inline bodyParser
// app.post("/save", bodyParser(), (rev) => {...});

Other Runtime (Bun / Node)

import { multipart, nhttp } from "nhttp-land";

const app = nhttp();

app.get("/", () => "hello, world");

// example upload
const upload = multipart.upload({
  name: "image",
  writeFile: Bun.write, /* or fs.writeFileSync */
});
app.post("/upload", upload, (rev) => {
  console.log(rev.file);
  console.log(rev.body);
  return "success upload";
});

app.listen(8000, () => {
  console.log("> Running on port 8000");
});

// if cfw or other runtime, just invoke app.handle
// export default { fetch: app.handle };

tsconfig

{
  "compilerOptions": {
    // if bun
    // "types": ["bun-types"],
    "lib": [
      "DOM",
      "DOM.Iterable",
      "ESNext"
    ]
  }
}

License

MIT