Router middleware for bragg.
$ npm install --save bragg-router
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();
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();
In order for you to use the router, you will have to add extra properties to your mapping template.
{
"resource-path": "$context.resourcePath",
"http-method": "$context.httpMethod"
}
Bragg will detect these properties and expose them as path
and method
properties in the middlewares.
Type: string
Values: get
post
put
delete
patch
head
update
HTTP-method to listen to.
Type: string
Action of the request. Accepts a matcher pattern.
Type: function
Functions to be executed the request matches the path
.
Returns a middleware function that can be used by bragg.
MIT © Sam Verschueren