ZijianHe / koa-router
Router middleware for koa.
AI Architecture Analysis
This repository is indexed by RepoMind. By analyzing ZijianHe/koa-router in our AI interface, you can instantly generate complete architecture diagrams, visualize control flows, and perform automated security audits across the entire codebase.
Our Agentic Context Augmented Generation (Agentic CAG) engine loads full source files into context on-demand, avoiding the fragmentation of traditional RAG systems. Ask questions about the architecture, dependencies, or specific features to see it in action.
Repository Overview (README excerpt)
Crawler viewkoa-router > Router middleware for koa • Express-style routing using , , , etc. • Named URL parameters. • Named routes with URL generation. • Responds to requests with allowed methods. • Support for and . • Multiple route middleware. • Multiple routers. • Nestable routers. • ES7 async/await support. Migrating to 7 / Koa 2 • The API has changed to match the new promise-based middleware signature of koa 2. See the koa 2.x readme for more information. • Middleware is now always run in the order declared by (or , etc.), which matches Express 4 API. Installation Install using npm: API Reference • koa-router • Router ⏏ • [new Router([opts])](#new_module_koa-router--Router_new) • _instance_ • .get|put|post|patch|delete|del ⇒ Router • .routes ⇒ function • [.use([path], middleware)](#module_koa-router--Router+use) ⇒ Router • .prefix(prefix) ⇒ Router • [.allowedMethods([options])](#module_koa-router--Router+allowedMethods) ⇒ function • [.redirect(source, destination, [code])](#module_koa-router--Router+redirect) ⇒ Router • .route(name) ⇒ Layer | false • [.url(name, params, [options])](#module_koa-router--Router+url) ⇒ String | Error • .param(param, middleware) ⇒ Router • _static_ • .url(path, params) ⇒ String Router ⏏ **Kind**: Exported class new Router([opts]) Create a new router. | Param | Type | Description | | --- | --- | --- | | [opts] | Object | | | [opts.prefix] | String | prefix router paths | **Example** Basic usage: router.get|put|post|patch|delete|del ⇒ Router Create methods, where *verb* is one of the HTTP verbs such as or . Match URL patterns to callback functions or controller actions using , where **verb** is one of the HTTP verbs such as or . Additionaly, can be used to match against all methods. When a route is matched, its path is available at and if named, the name is available at Route paths will be translated to regular expressions using path-to-regexp. Query strings will not be considered when matching requests. Named routes Routes can optionally have names. This allows generation of URLs and easy renaming of URLs during development. Multiple middleware Multiple middleware may be given: Nested routers Nesting routers is supported: Router prefixes Route paths can be prefixed at the router level: URL parameters Named route parameters are captured and added to . The path-to-regexp module is used to convert paths to regular expressions. **Kind**: instance property of Router | Param | Type | Description | | --- | --- | --- | | path | String | | | [middleware] | function | route middleware(s) | | callback | function | route callback | router.routes ⇒ function Returns router middleware which dispatches a route matching the request. **Kind**: instance property of Router router.use([path], middleware) ⇒ Router Use given middleware. Middleware run in the order they are defined by . They are invoked sequentially, requests start at the first middleware and work their way "down" the middleware stack. **Kind**: instance method of Router | Param | Type | | --- | --- | | [path] | String | | middleware | function | | [...] | function | **Example** router.prefix(prefix) ⇒ Router Set the path prefix for a Router instance that was already initialized. **Kind**: instance method of Router | Param | Type | | --- | --- | | prefix | String | **Example** router.allowedMethods([options]) ⇒ function Returns separate middleware for responding to requests with an header containing the allowed methods, as well as responding with and as appropriate. **Kind**: instance method of Router | Param | Type | Description | | --- | --- | --- | | [options] | Object | | | [options.throw] | Boolean | throw error instead of setting status and header | | [options.notImplemented] | function | throw the returned value in place of the default NotImplemented error | | [options.methodNotAllowed] | function | throw the returned value in place of the default MethodNotAllowed error | **Example** **Example with Boom** router.redirect(source, destination, [code]) ⇒ Router Redirect to URL with optional 30x status . Both and can be route names. This is equivalent to: **Kind**: instance method of Router | Param | Type | Description | | --- | --- | --- | | source | String | URL or route name. | | destination | String | URL or route name. | | [code] | Number | HTTP status code (default: 301). | router.route(name) ⇒ Layer | false Lookup route with given . **Kind**: instance method of Router | Param | Type | | --- | --- | | name | String | router.url(name, params, [options]) ⇒ String | Error Generate URL for route. Takes a route name and map of named . **Kind**: instance method of Router | Param | Type | Description | | --- | --- | --- | | name | String | route name | | params | Object | url parameters | | [options] | Object | options parameter | | [options.query] | Object | String | query options | **Example** router.param(param, middleware) ⇒ Router Run middleware for named route parameters. Useful for auto-loading or validation. **Kind**: instance method of Router | Param | Type | | --- | --- | | param | String | | middleware | function | **Example** Router.url(path, params [, options]) ⇒ String Generate URL from url pattern and given . **Kind**: static method of Router | Param | Type | Description | | --- | --- | --- | | path | String | url pattern | | params | Object | url parameters | | [options] | Object | options parameter | | [options.query] | Object | String | query options | **Example** Contributing Please submit all issues and pull requests to the alexmingoia/koa-router repository! Tests Run tests using . Support If you have any problem or suggestion please open an issue here.