I am looking for a way to simplify route registration without manually writing out
app.get('/', function (req, res, next) { });
each time. I want to automate this process by passing in a router object like the one below...
{
path: '',
method: 'GET',
function: 'test'
}
Everything works fine until it comes to actually executing the function.
The problem is with how I am attempting to call the function. Instead of executing it, I am only referencing it.
Here is the code responsible for calling the function.
this.routes.forEach((route: IRouter) => {
const path = this.basePath + '/' + route.path;
const requestMethod = route.method.toLowerCase();
this.app[requestMethod](path, this[route.function]());
});
As you can see in the this.app
line, I am trying to call the function without passing in necessary parameters like res, req, next
.
Is there a way in Express to access these parameters through the app instance, since removing the ()
from the function call doesn't actually execute it?
Any assistance would be highly appreciated.