ES6 Class, pass function as parameter to register expressjs routes
Use bind method to bind the scope, like this
this.app.get(APISLUG + this.name, this.all.bind(this));
You can set an arrow function as a property on the class. Arrow functions lexically binds the this
value (https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions).
In your example:
export default class BaseController {
...
all = (req, res, next) => { // This is the only line that changed
...
}
}
Note that arrow functions on classes are not standard ES6 syntax, but will probably come with ES7 (some notes about that in comments here: https://stackoverflow.com/a/31362350/2054731). You may need to configure your project to be able to use this and/or other ES7 features.