@intrnl/srv-router
TypeScript icon, indicating that this package has built-in type declarations

0.2.1 • Public • Published

srv-router

Router middleware for Srv

Usage

npm install @intrnl/srv-router
# pnpm install @intrnl/srv-router
# yarn add @intrnl/srv-router
import { Application } from '@intrnl/srv';
import { Router } from '@intrnl/srv-router';

let app = new Application();
let router = new Router();

router.route('GET', '/', ({ response }) => {
  response.body = 'Hello!';
});

app.use(router.handler);

Routing

Commonly used patterns for path matching is supported

router.route('GET', '/users/:id', ({ request, response }) => {
  response.body = `Hello, ${request.params.id}!`;
});

Mounting

You can have middlewares that runs on a specific endpoint prefix, it is recommended that you add them first before adding any specific routes

Patterns are not supported, it will be ignored and be treated as regular path.

router.mount('/user', (ctx, next) => {
  // this will run on on every request that starts with /user/
  return next();
});

Mounting another router is supported, allowing for subrouting

let app = new Application();
let forum = new Router();
let main = new Router();

forum.route('GET', '/', ({ response }) => {
  response.body = 'Welcome to the forum!';
});

main.mount('/forum', forum.handler);

main.route('GET', '/', ({ response }) => {
  response.body = 'Welcome to the main page!';
});

app.use(main.handler);

Readme

Keywords

none

Package Sidebar

Install

npm i @intrnl/srv-router

Weekly Downloads

8

Version

0.2.1

License

MIT

Unpacked Size

6.12 kB

Total Files

5

Last publish

Collaborators

  • intrnl