Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
@koa/router
Advanced tools
@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.
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);
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 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 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. Maintained by Forward Email and Lad.
app.get
, app.put
, app.post
, etc.)OPTIONS
requests with allowed methods405 Method Not Allowed
and 501 Not Implemented
async/await
support.use()
(or .get()
,
etc.), which matches Express 4 API.npm:
npm install @koa/router
npm install --save-dev @types/koa__router
See API Reference for more documentation.
Name |
---|
Alex Mingoia |
@koajs |
Imed Jaberi |
MIT © Alex Mingoia
FAQs
Router middleware for koa. Maintained by Forward Email and Lad.
The npm package @koa/router receives a total of 978,797 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 0 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
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.