Skip to content

Latest commit

 

History

History
69 lines (45 loc) · 1.08 KB

readme.md

File metadata and controls

69 lines (45 loc) · 1.08 KB

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();
    }
}