bragg-router

Router middleware for bragg

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
bragg-router
122.1.05 years ago7 years agoMinified + gzip package size for bragg-router in KB

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