koa-cond
v0.0.1
Published
conditionally include koa middleware
Downloads
1
Readme
koa-cond
Conditionally run middleware in Koa 2
Install
npm install --save koa-cond
API
condition
a function which receives koa context and returns booleanifMiddleware
middleware to run if condition returns trueelseMiddleware
(optional) middleware to run if condition returns false
Usage
const Koa = require('koa'),
bodyParser = require('koa-bodyparser'),
rawBody = require('koa-rawbody'),
cond = require('koa-cond');
const app = Koa();
const rawBody = (ctx, next) => {
/* capture request as buffer */
ctx.request.rawBody = await new Promise((resolve) => {
const chunks = [];
ctx.request.on('data', chunk => {
chunks.push(chunk);
});
ctx.req.on('end', () => {
resolve(Buffer.concat(chunks));
});
});
}
app.use(cond(
(ctx) => ctx.request.is('application/octet-stream'),
rawBody,
bodyParser
));
// routes, etc.
app.listen(3000);
License
MIT