Skip to main content

logo

NHttp

An Simple web-framework for Deno and Friends.

ci coverage codefactor denoland deps size licence prs nestland


Features

  • Crazy Fast.
  • Easy to use.
  • Cross runtime support (Deno, Bun, Node, etc).
  • Low overhead & True handlers (no caching anything).
  • Small & Zero deps.
  • Middleware support.
  • Sub router support.
  • Template engine support (jsx, ejs, nunjucks, eta, pug, ..etc).
  • Return directly on handlers.
  • Auto parses the body (json / urlencoded / multipart / raw).

See Examples

See Benchmark

Installation

deno.land

import nhttp from "https://deno.land/x/nhttp@1.2.4/mod.ts";

deno-npm

import nhttp from "npm:nhttp-land@1.2.4";

nest.land

import nhttp from "https://x.nest.land/nhttp@1.2.4/mod.ts";

npm/yarn

npm i nhttp-land

// or

yarn add nhttp-land
// module
import nhttp from "nhttp-land";

// commonjs
const nhttp = require("nhttp-land").default;

Usage

import nhttp from "https://deno.land/x/nhttp@1.2.4/mod.ts";

const app = nhttp();

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

app.get("/cat", () => {
  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 / raw.

note: nhttp automatically parses the body.

const app = nhttp();

// if you 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)

for nodejs, requires v18.14.0 or higher. cause it uses Web Stream API like ReadableStream.

import nhttp from "nhttp-land";

const app = nhttp();

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

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

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

tsconfig (Bun / Node)

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

Jsx

/** @jsx n */
/** @jsxFrag n.Fragment */

import { n, Helmet, renderToHtml, FC } from "https://deno.land/x/nhttp@1.2.4/lib/jsx.ts";
import nhttp from "https://deno.land/x/nhttp@1.2.4/mod.ts";

const Home: FC<{ title: string }> = (props) => {
  return (
    <>
      <Helmet>
        <title>{props.title}</title>
      </Helmet>
      <h1>Home Page</h1>
    </>
  );
};

const app = nhttp();

app.engine(renderToHtml);

app.get("/", () => <Home title="welcome jsx" />);

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

License

MIT