nodejs难点

1. compose

compose(middlewares) // middleware应该是回调, 所有用async-await. middlewares = [async (ctx, next)=>{ log1, await next(), log2 }, async (ctx, next) => { log3, await next(), log4 } ]

2. getter/setter 可以使得获取值变短

createContext(req, res) {
        const ctx = Object.create(context)
        ctx.request = Object.create(request)
        ctx.response = Object.create(response)

        ctx.req = ctx.request.req = req
        ctx.res = ctx.response.res = res
        return ctx
    }
这样在resposne、requet, conntext里面定义快速set值和get值。
3. 
function(...[first,...second])
如果不知道多少参数可以: function(....args) //function(){ arguments }
这是为了解决多数餐, 特别需要对第一个参数进行特别处理的。 如
const compose1 = (...[first,...second]) => (...args) =>{
    console.log(first, second);
    let ret = first(...args)
    second.forEach(fn=>{
        ret = fn(ret)
    })
    return ret;
}
 

猜你喜欢

转载自www.cnblogs.com/connie313/p/13369560.html