Router
Rover middleware needs a router to operate. Router is essentially an array of Route
object.
The route interface
The route object is simple, it looks like this:
type Route = {
path: string;
handler(request: Request): Promise<Response>;
method?: string;
};
Here,
path
is the url path which starts with a/
for instance/blog
handler
is the handler function which acceptsrequest
as a param which is useful for inspecting requestsmethod
as the name suggests the http method likePOST
orGET
. By default the method will beGET
, if it's not explicitly defined.
Create Router
Creating router is simple, just call the built-in Router
function and pass in an array of Route
For instance here's a demo router
router.js
const router = Router([
{
path: "/",
handler: home_handler,
method: "GET",
},
{
path: "/blogs",
handler: blog_handler,
method: "GET",
},
{
path: "/auth/login",
handler: login_handler,
method: "POST",
},
]);
That's it, it's as simple as that. No need to make it any complex further. This design helps you keep a separate route tree for your application.