middleware.js 540 Bytes
/**
 * 路由上下文构造器,传入上下文
 * @author: feng.chen<feng.chen@yoho.cn>
 * @date: 2017/04/13
 */

const Context = require('./context');

module.exports = (Type, action) => {
    return (req, res, next) => {
        let instance = new Type();

        if (instance instanceof Context) {
            instance.setContext({
                req,
                res
            });
            if ((typeof instance[action]) === 'function') {
                instance[action](req, res, next);
            }
        }
    };
};