Child Routers

Child routers are useful to scale up your app and split it's functionally into smaller working units:

Using app.child

You can create a child router using app.child(path):

app.child("/child").get("/", () => {
return new TextBody("GET /child");
});

Note: Order of registering child routers is important. If router A has /a and router B has /a/b, router B should be registered before A because it would catch all paths with /a.

Router Class

You can also create a router using the Router class, and attach it using app.child:

const { Router } = require("routex");
const nameRouter = new Router();
nameRouter.get("/", () => {
return new TextBody("GET /name");
});
app.child("/name", nameRouter);

Middlewares

You can also apply middlewares when creating child routers:

const firstNameMiddleware = (ctx) => {
ctx.data.firstName = "john";
};
const lastNameMiddleware = (ctx) => {
ctx.data.lastName = "smith";
};
const { Router } = require("routex");
const childRouter = new Router();
app.child("/firstName", childRouter).middleware(firstNameMiddleware);
app
.child("/name", childRouter)
.middleware([firstNameMiddleware, lastNameMiddleware]);
// Or
app.child("/firstName").middleware(firstNameMiddleware);
app.child("/name").middleware([firstNameMiddleware, lastNameMiddleware]);
// Or
app.child("/firstName", [childRouter, firstNameMiddleware]);
app.child("/name", [childRouter, [firstNameMiddleware, lastNameMiddleware]]);
// Or
app.child("/firstName", [null, firstNameMiddleware]);
app.child("/name", [null, [firstNameMiddleware, lastNameMiddleware]]);