带控制器/管理器的ExpressJS模块

时间:2018-04-12 08:19:55

标签: node.js express

我正在尝试使用express创建一个API,但是在初始设置方面遇到了困难,我想为控制器和放大器设置单独的类。经理,但在尝试注册我的路线时不断收到以下错误:

Cannot read property 'post' of undefined

到目前为止,这是我的设置:

app.js

const express = require('express');
const app = express();
const port = process.env.PORT || 3000;
var UserController = require('./lib/controllers/user/user.controller');

userController = new UserController();

app.use('/user', userController);

app.listen(port);
console.log('Listening on port 3000...');

module.exports = app;

user.controller.js

const {register, login} = require('../routes').user;
var userManager = require('../../managers/user/user.manager');
const express = require('express');
const expressApp = express.Router();

class UserController {

constructor(
    ) {
        userManager = new userManager();    
        this.expressApp.post(register, this.register.bind(this));
    }

    register(request, response) {
        return userManager
            .register(request)
            .then(result => response.status(statusCodes.OK).json(result))
            .catch(e);
    }
}

module.exports = UserController;

1 个答案:

答案 0 :(得分:1)

UserController的{​​{1}}。

您尝试使用constructorthis.expressApp未定义。