bragg-router
v2.1.0
Published
Router middleware for bragg
Downloads
4,777
Maintainers
Readme
bragg-router
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