NHttp

License deno.land PRs Welcome deps badge cache badge nest.land

An Simple http framework for Deno, Deno Deploy and Cloudflare Workers.

Note: Deno native HTTP/2 Hyper requires Deno version 1.9.0 or higher.

Features

  • Fast.
  • HTTP/2 support.
  • Middleware support.
  • Router support.
  • Includes body parser (json, urlencoded, raw, multipart).
  • Return directly on handlers.
  • No third party modules and no std/lib by default.
  • Easy deploy to Deno Deploy and Cloudflare Workers.

See examples

Benchmark

Here the simple benchmark.

autocannon -c 100 http://localhost:8080/hello

Name Req/sec Throughput
Native 21433 2.5 MB
NHttp 21127 2.5 MB
std/http 14569 626 KB

Note: maybe not relevant if compared with std/http or other Deno framework using std/http. nhttp uses native deno http.

for cloudflare workers visit => https://nhttp.deno.dev/docs/usage/cloudflare-workers

Installation

deno.land

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

nest.land

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

Usage

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

const app = new NHttp();

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

  // or
  // return "Hello World";
  // return { name: "john" };
  // return new Response("Hello World");
});

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

METHOD => get | post | put | patch | delete | any | head | options.

app[METHOD](path: string, (rev: RequestEvent) => { // code });

// or app[METHOD](path: string, ...handlers);

Run

deno run --allow-net yourfile.ts

Custom Server

import { serve } from "https://deno.land/std@0.119.0/http/server.ts";
import { NHttp } from "https://deno.land/x/nhttp@1.1.5/mod.ts";

const app = new NHttp();
app.get("/", ({ response }) => {
  return response.send("Hello Custom");
});

serve((request, conn) => app.handleEvent({ request, conn }));

Middleware Example

app.use(...handlers) or app.use([fn1, fn2])

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

new NHttp()
  .use((rev, next) => {
    rev.foo = "foo";
    return next();
  })
  .get("/", ({ foo }) => {
    return foo;
  })
  .listen(8080);

Router Example

app.use(router | router[]) or app.use(basePath, router | router[])

import { NHttp, Router } from "https://deno.land/x/nhttp@1.1.5/mod.ts";

//user router example with base
const user = new Router({ base: "/user" }); // base optional
user.get("/", ...handlers);

//item router example without base
const item = new Router();
item.get("/item", ...handlers);

new NHttp()
  // register router
  .use("/api/v1", [user, item])
  .listen(8080);

now, you can access with http://localhost:8080/api/v1/user

Usage With Class

import {
  NHttp,
  RequestEvent,
  Router,
} from "https://deno.land/x/nhttp@1.1.5/mod.ts";

class UserRouter extends Router {
  constructor() {
    super({ base: "/user" });
    this.get("/", this.findAll);
    this.get("/:id", this.findById);
  }

  private findAll(rev: RequestEvent) {
    return "Hello User";
  }

  private findById(rev: RequestEvent) {
    return rev.params.id;
  }
}

class Application extends NHttp {
  constructor() {
    super();
    this.use("/api/v1", [
      new UserRouter(),
      // other router
    ]);
  }
}

new Application().listen(8080);

visit http://localhost:8080/api/v1/user

Full Documentation NHttp

https://nhttp.deno.dev

or

https://nhttp.herudi.workers.dev

Want to contribute to this project? I gladly welcome it.

  • Please fork.
  • Create a branch.
  • Commit changes (before commit, please format the code with the command deno fmt in the src folder).
  • Push to the created branch.
  • Make a PR (Pull Requests).
  • Thanks.

List

  • Server App
  • Middleware
  • Router
  • Body Parser
  • Examples
  • Doc
  • Deno lint
  • Unit Test

License

MIT