JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1257
  • Score
    100M100P100Q110238F
  • License MIT

Router middleware for bragg

Package Exports

  • bragg-router

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (bragg-router) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

bragg-router

Build Status Coverage Status

Router middleware for bragg.

Install

$ npm install --save bragg-router

Usage

const app = require('bragg')();
const router = require('bragg-router')();

router.get('/', ctx => {
    ctx.body = 'Home';
});

router.get('/user/{id}', ctx => {
    ctx.body = `Retrieve user with id ${ctx.request.params.id}`;
});

app.use(router.routes());

exports.handler = app.listen();

Multiple handlers

When a handler returns a promise, that promise will be resolved first. The result of following example will be Foo Bar.

const app = require('bragg')();
const router = require('bragg-router')();

router.get('/',
    () => Promise.resolve('Foo');
    (ctx, result) => {
        ctx.body = `${result} Bar`;
    }
);

app.use(router.routes());

exports.handler = app.listen();

API

verb(path, ...middlewares)

verb

Type: string
Values: get post put delete patch head update

HTTP-method to listen to.

path

Type: string

Action of the request. Accepts a matcher pattern.

middlewares

Type: function

Functions to be executed the request matches the path.

routes()

Returns a middleware function that can be used by bragg.

License

MIT © Sam Verschueren