鍍金池/ 問答/HTML/ ExpressJS的一個問題

ExpressJS的一個問題

路由

router.post('/login', require('../app/controller/admin/login').login);

控制器

let BaseController = require('../BaseController');

class Login extends BaseController {
    // 登陸
    login() {
        console.log(this.req); // undefiend
    }
}

// here here here here here here
module.exports = new Login(req, res, next);

基類

class BaseController {

    constructor(req, res, next) {

        console.log('-------------', req);

        this.req  = req;
        this.res  = res;
        this.next = next;
        this.data = req.body || req.query; // 獲取所有數(shù)據(jù)
    }
}
module.exports = BaseController();

請問下大家 路由的req 等參數(shù)怎樣傳入控制器里,在導(dǎo)入模塊的時候 new Login(req, res, next)???

謝謝大家

回答
編輯回答
詆毀你
  1. 控制器里面的代碼有語法錯誤
  2. 基類里面class關(guān)鍵字使用錯誤
2017年9月15日 21:55
編輯回答
敢試

路由

router.post('/login', (req, res, next) => {
    require('../app/controller/admin/login')(req, res, next).login
});

控制器輸出函數(shù)

let BaseController = require('../BaseController');
class Login extends BaseController {
    // 登陸
    login() {
        console.log(this.req); // undefiend
    }
}
// here here here here here here
module.exports = (req, res, next) => {
    return new Login(req, res, next)
};

基類輸出不要括號

class BaseController {
    constructor(req, res, next) {
        console.log('-------------', req);
        this.req  = req;
        this.res  = res;
        this.next = next;
        this.data = req.body || req.query; // 獲取所有數(shù)據(jù)
    }
}
module.exports = BaseController;
2017年10月17日 10:53