Reputation: 272
I am new to expressJS, nodeJS and hope some kind souls would enlighten me on routing.
I have this line in app.js
var core = require('./routes/core/'),
app.get ('/core/:base/:methodfunc', core);
The end result I would like to achieve is when i pass in /core/testmethod/test from the URL, I am able to access test function in testmethod file
The routing :base param is to route to testmethod.js and :methodfunc method is the exported function in testmethod
This is my main code in /core index.js
module.exports = function(req, res) {
var base = req.params.base;
var methodfunc = req.params.methodfunc;
var basePage = require('./' + base);
if (basePage) {
if (methodfunc) {
basePage.methodfunc(req, res); //there is something very wrong here
} else {
fail();
}
} else {
fail();
}
function fail() {
res.send(404);
}
}
testmethod.js function
exports.test = function(req, res) {
res.json({
name: 'hello there'
});
};
THANKS!
Upvotes: 0
Views: 62
Reputation: 4604
Looks like you want to call the function by name so it would be:
basePage[methodfunc](req, res);
Upvotes: 1