Skip to main content

Intro

nhttp ci License deno.land PRs Welcome deps badge minzip min codecov CodeFactor

Features

  • Focus on simple and easy to use.
  • Fast Performance. One of the fastest Frameworks.
  • Cross runtime support (Deno, Node, Bun, etc).
  • Low overhead & True handlers (no caching anything).
  • Built-in Middleware.
  • 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

starting v1.3.0, requires Deno 1.35 or higher.

CLI

Deno

deno run -Ar npm:create-nhttp

Npm

npm create nhttp@latest

Manual Installation

deno.land

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

deno-npm

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

npm/yarn

npm i nhttp-land

// or

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

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

Simple Usage

import nhttp from "https://deno.land/x/nhttp@1.3.25/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");
});

Return direcly supported => Response | String | JSON | Number | ReadableStream | Uint8Array | Blob | null

Return directly support promise (async/await).

app.get("/cat", async () => {
return await Promise.resolve("hello");
});

Run

deno run -A myapp.ts

Middleware

All Route built-in middleware.

Example 1

const app = nhttp();

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

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

Example 2

const app = nhttp();

type Foo = { count: number };

app.get<Foo>("/foo", (rev, next) => {
rev.count = 0;
return next();
});
app.get<Foo>("/foo", (rev, next) => {
rev.count++;
return next();
});
app.get<Foo>("/foo", (rev, next) => {
rev.count++;
return next();
});
app.get<Foo>("/foo", (rev) => rev.count);

// GET/foo => 2

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 (Node / Bun)

for nodejs, requires v18.0.0 or higher. cause it uses Fetch API.

import nhttp from "nhttp-land";

const app = nhttp();

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

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

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

Coudflare Workers

import nhttp from "nhttp-land";

const app = nhttp();

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

export default app.module();

// for other just invoke app.handle
// export default app.handle;

tsconfig (Bun / Node)

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

Jsx

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

import nhttp from "https://deno.land/x/nhttp@1.3.25/mod.ts";
import { n, FC, renderToHtml, Helmet } from "https://deno.land/x/nhttp@1.3.25/lib/jsx.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