What is @koa/router?
@koa/router is a powerful and flexible routing library for Koa, a popular web framework for Node.js. It allows developers to define routes and handle HTTP requests with ease, providing a clean and modular way to build web applications and APIs.
What are @koa/router's main functionalities?
Basic Routing
This code demonstrates how to set up a basic route using @koa/router. It creates a Koa application, defines a router, and sets up a GET route for the root URL that responds with 'Hello World!'.
const Koa = require('koa');
const Router = require('@koa/router');
const app = new Koa();
const router = new Router();
router.get('/', (ctx, next) => {
ctx.body = 'Hello World!';
});
app.use(router.routes());
app.use(router.allowedMethods());
app.listen(3000);
Route Parameters
This code demonstrates how to use route parameters with @koa/router. It sets up a route that captures a user ID from the URL and responds with the user ID.
const Koa = require('koa');
const Router = require('@koa/router');
const app = new Koa();
const router = new Router();
router.get('/users/:id', (ctx, next) => {
const userId = ctx.params.id;
ctx.body = `User ID: ${userId}`;
});
app.use(router.routes());
app.use(router.allowedMethods());
app.listen(3000);
Middleware
This code demonstrates how to use middleware with @koa/router. It defines a logger middleware that logs the HTTP method and URL of each request, and applies it to a route.
const Koa = require('koa');
const Router = require('@koa/router');
const app = new Koa();
const router = new Router();
const logger = async (ctx, next) => {
console.log(`${ctx.method} ${ctx.url}`);
await next();
};
router.get('/', logger, (ctx, next) => {
ctx.body = 'Hello World!';
});
app.use(router.routes());
app.use(router.allowedMethods());
app.listen(3000);
Nested Routes
This code demonstrates how to set up nested routes with @koa/router. It creates a nested router and mounts it under a parent route.
const Koa = require('koa');
const Router = require('@koa/router');
const app = new Koa();
const router = new Router();
const nestedRouter = new Router();
nestedRouter.get('/info', (ctx, next) => {
ctx.body = 'Nested Route Info';
});
router.use('/nested', nestedRouter.routes(), nestedRouter.allowedMethods());
app.use(router.routes());
app.use(router.allowedMethods());
app.listen(3000);
Other packages similar to @koa/router
koa-router
koa-router is another popular routing library for Koa. It provides similar functionality to @koa/router, including support for route parameters, middleware, and nested routes. However, @koa/router is a more modern and actively maintained fork of koa-router.
express
Express is a widely-used web framework for Node.js that includes built-in routing capabilities. While it is not specifically designed for Koa, it offers similar routing features and is known for its simplicity and flexibility. Developers who prefer a more comprehensive framework might choose Express over Koa and @koa/router.
hapi
Hapi is a rich framework for building applications and services in Node.js. It includes a powerful routing system that supports route parameters, middleware, and nested routes. Hapi is known for its configuration-driven approach and extensive plugin ecosystem, making it a strong alternative to Koa and @koa/router.
Router middleware for Koa.
- Express-style routing (
app.get
, app.put
, app.post
, etc.) - Named URL parameters
- Named routes with URL generation
- Responds to
OPTIONS
requests with allowed methods - Support for
405 Method Not Allowed
and 501 Not Implemented
- Multiple route middleware
- Multiple and nestable routers
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
.use()
(or .get()
,
etc.), which matches Express 4 API.
Installation
npm i @koa/router
yarn add @koa/router
Contributing
Please submit all issues and pull requests to the koajs/router repository!
Support
If you have any problem or suggestion please open an issue here.
Call for Maintainers
This module is forked from the original koa-router due to its lack of activity. koa-router
is the most widely used router module in the Koa community and we need maintainers. If you're interested in fixing bugs or implementing new features feel free to open a pull request. We'll be adding active contributors as collaborators.
Thanks to the original authors @alexmingoia and the original team for their great work.
License
MIT