Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@koa/router

Package Overview
Dependencies
Maintainers
8
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@koa/router

Router middleware for koa. Provides RESTful resource routing.

  • 10.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
764K
decreased by-30.48%
Maintainers
8
Weekly downloads
 
Created

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

Keywords

FAQs

Package last updated on 20 Jul 2021

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc