koa-lambda-middleware

1.0.7-babel • Public • Published

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 = {

    /*
        request:
        POST http://localhost:3333/foo/bar HTTP/1.1
        content-type: application/json
        {
            "args":[
                2,
                3
            ]
        }
        
        response:
        {
            "c": 5
        }
    */
    // It is a pure function.

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

    

    /*
        request:
        POST http://localhost:3333/foo/baz HTTP/1.1
        content-type: application/json

        response:
        baz ok!
    */
    async baz(){
        // use hooks
        let ctx = useContext(); //koa ctx
        let next = useNext();   //koa next

        ctx.body = 'baz ok!'

        await next();
    }
}

Package Sidebar

Install

npm i koa-lambda-middleware

Weekly Downloads

0

Version

1.0.7-babel

License

MIT

Unpacked Size

29.5 kB

Total Files

8

Last publish

Collaborators

  • tangoboy