1

I am doing some routing stuff and I am also using express so I'll need request and response later in controllers but I wanted to just inherit it from base controller instead of typing it in each new instance as arguments so here I've got code when I am including two controllers

loadController(controller, method)
{
    var Base = require.main.require("./App/Base/Controller"),
        BaseController = new Base(this.req, this.res),
        NewController = require.main.require("./App/Controllers/" + controller),
        Controller = new NewController();

    Controller[method]();

}

Here's the base controller:

module.exports = class Controller{

    constructor(req, res)
    {
        this.req = req;
        this.res = res;
    }

}

And what I want to ask is it possible that I can get this.req and this.res in any other controllers included such as:

var Controller = require.main.require("./App/Base/Controller");

module.exports = class indexController extends Controller{

    indexAction()
    {
        // I want `this.req` and `this.res` there
    }

}
miloszowi
  • 36
  • 5
  • `I want \`this.req\` and \`this.res\` there` - yes, you have them - but the code you show doesn't have any way of instantiating them - with the code you've shown, what do you expect them to be – Jaromanda X Jun 07 '17 at 06:10
  • Whenever I want to use `this.res` it shows `TypeError: Cannot read property 'send' of undefined` – miloszowi Jun 07 '17 at 06:12
  • 1
    perhaps you need to show more code ... because as it stands they will be `undefined` - because ... well ... how do you expect them to become defined with what you've shown so far – Jaromanda X Jun 07 '17 at 06:13

0 Answers0