Security News
Internet Archive Hacked, 31 Million Record Compromised
The Internet Archive's "Wayback Machine" has been hacked and defaced, with 31 millions records compromised.
koa-router
Advanced tools
koa-router is a powerful routing middleware for Koa, a next-generation web framework for Node.js. It allows you to define routes for your web application, handle HTTP methods, and manage middleware in a clean and organized manner.
Basic Routing
This code demonstrates how to set up a basic route using koa-router. When a GET request is made to the root URL '/', it 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. When a GET request is made to '/users/:id', it responds with the user ID provided in the URL.
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. The logger middleware logs the HTTP method and URL of each request before passing control to the next middleware or route handler.
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 create nested routes with koa-router. The nestedRouter handles requests to '/nested/info' and responds with 'Nested Route Info'.
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);
Express is a fast, unopinionated, minimalist web framework for Node.js. It provides robust routing capabilities similar to koa-router but is more widely used and has a larger ecosystem of middleware and plugins.
Hapi is a rich framework for building applications and services. It provides powerful configuration-based routing and extensive support for building scalable and maintainable applications. Compared to koa-router, Hapi offers more built-in features and a more opinionated structure.
Restify is a framework specifically designed for building RESTful web services. It provides a similar routing mechanism to koa-router but is optimized for building APIs with a focus on performance and scalability.
Express-like HTTP routing for koa
Examples and Other Documentation
koa-router
requires node 8 or above.
npm install -S koa-router
const app = new Koa();
const router = new Router();
router.get('/hello', async (ctx) => {
ctx.body = 'world';
});
app.use(router.routes());
app.listen(3000);
router.get
, router.put
, router.post
, etcconst router = new Router();
router.patch('/users/:id', (ctx) => ...);
router.get('/users', (ctx) => ...);
/users/:id
-> ctx.params.id
)const router = new Router();
router.get('/articles/:category', (ctx) => {
// ctx.params.category available
});
const router = new Router();
router.get('article', '/article/:id', (ctx) => ...);
router.path('article', 3);
// => /articles/3
const rootRouter = new Router();
const authorRouter = new Router();
const bookRouter = new Router();
bookRouter.get('/', (ctx) => ...);
authorRouter.get('/:id', (ctx) => ...);
authorRouter.nest('/:id/books', bookRouter);
rootRouter.nest('/books', bookRouter);
rootRouter.nest('/authors', authorRouter);
rootRouter.routes();
// =>
// /books
// /authors/:id
// /authors/:id/books
const router = new Router();
router.get('/users/:id', async (ctx) => {
ctx.state.user = await User.find(ctx.params.id);
});
OPTIONS
requests with allowed methods405 Method Not Allowed
and 501 Not Implemented
handlerspath-to-regexp
Koa Router version 7 and 8 are only compatible with koa 2 and do not work with koa 1 or any other koa 1 middlewares
See the UPGRADING.md
Please submit all issues and pull requests to the alexmingoia/koa-router repository!
Generate coverage reports with npm run coverage
Generate the docs with npm run docs
Run the linter with npm lint
Run tests using npm test
If you identify an issue with this software or have a suggestion, please open an issue here.
FAQs
Router middleware for koa. Maintained by Forward Email and Lad.
The npm package koa-router receives a total of 356,354 weekly downloads. As such, koa-router popularity was classified as popular.
We found that koa-router demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The Internet Archive's "Wayback Machine" has been hacked and defaced, with 31 millions records compromised.
Security News
TC39 is meeting in Tokyo this week and they have approved nearly a dozen proposals to advance to the next stages.
Security News
Our threat research team breaks down two malicious npm packages designed to exploit developer trust, steal your data, and destroy data on your machine.