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

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

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

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