1.0.5 • Published 2 years ago

koa-lambda-middleware v1.0.5

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

koa-lambda-middleware

A simple functional middleware that can use hooks for koa.

中文文档

usage

const Koa = require('koa');
const koaBody = require('koa-body');
const koaLambda = require('koa-lambda-middleware');
const app = new Koa();


app.use(koaBody())
    .use(koaLambda({}, app));

app.listen(3333);

file /src/foo.js

const { useContext, useNext } = require('koa-lambda-middleware');

module.exports = {

    /*
        POST http://localhost:3333/foo/bar HTTP/1.1
        content-type: application/json
        {
            "args":[
                2,
                3
            ]
        }
    */

    bar(a, b){
        return {
            c : a + b;
        }
    },

    

    /*
        POST http://localhost:3333/foo/baz HTTP/1.1
        content-type: application/json
    */
    async baz(){
        // use hooks
        let ctx = useContext(); //koa ctx
        let next = useNext();   //koa next

        ctx.body = 'baz ok!'

        await next();
    }
}