Fast router handler for Deno server and Deploy.
Raptor implemented to nhttp
- Middleware support.
- Sub Router support.
import { serve } from "https://deno.land/std@0.118.0/http/server.ts";
import { JsonResponse, raptor } from "https://deno.land/x/raptor@0.0.8/mod.ts";
serve(
raptor()
.make("GET", () => new Response("Hello World"))
.make("GET/hello/:name", (req) => new Response(`Hello ${req.params.name}`))
.make("GET/hello-json", () => new JsonResponse({ name: "raptor" }))
.resolve,
);
console.log("Raptor was here !!");
deno run --allow-net file.ts
and visit http://localhost:8000
with path /
or /hello-json
or
/hello/yourname
Make everything with raptor().make(verb, ...fns)
.
the verb is =>
GET | POST | DELETE | PUT | PATCH | OPTIONS | HEAD | ANY | ROUTER | WARE | ERROR | 404
and path.
serve(
raptor()
.make("GET/hello/:name", (req) => new Response(`Hello ${req.params.name}`))
.resolve,
);
serve(
raptor()
.make("WARE", (req, next) => {
req.foo = "foo";
return next();
})
.make("GET/hello", (req) => new Response(`Hello ${req.foo}`))
.resolve,
);
serve(
raptor()
.make("GET/hello", (req, next) => {
req.foo = "foo";
return next();
}, (req) => {
return new Response(`Hello ${req.foo}`);
})
.resolve,
);
const router = raptor.createRouter();
router.make("GET/user", () => new Response("Hello from router"));
serve(
raptor()
.make("ROUTER/api/v1", [router])
.resolve,
);
// visit http://localhost:8000/api/v1/user
Global error response.
serve(
raptor()
// example
.make(
"ERROR",
(err, req) => new Response(err.message, { status: err.status || 500 }),
)
.resolve,
);
Global 404 response.
serve(
raptor()
// example
.make("404", (req) => new Response("404 not found url", { status: 404 }))
.resolve,
);