Using Router
@vclight/router is an official middleware package that maps URL paths to handlers.
Install
shell
npm install @vclight/routershell
yarn add @vclight/routershell
pnpm add @vclight/routerCreate and apply a router instance
typescript
import VCLight from "vclight";
import VCLightRouter from "@vclight/router";
const app = new VCLight();
const router = new VCLightRouter();
app.use(router);Add routes
typescript
router.on("/", async (_request, response) => {
response.contentType = "text/plain";
response.response = "Hello VCLight";
});
router.pattern(/^\/users\/\d+\/?$/, async (request, response) => {
response.response = {
path: request.url,
method: request.method
};
});Router options
typescript
const router = new VCLightRouter({
buildInRouters: { _404: true },
use404Router: true
});buildInRouters._404: whether to register a built-in/404/handler (defaulttrue)use404Router: whether to return the/404/handler when no route matches (defaulttrue)