I have defined the following express route(s) in server.js:
app.get('/adfeed', adfeed.findAll);
app.get('/subscriptions', subscriptions.findAll);
app.get('/cronjob/match', cronjob.match);
Function called when performing GET on /adfeed is:
exports.findAll = function(req, res) {
mongo.Db.connect(mongoUri, function (err, db) {
db.collection('adfeed', function(er, collection) {
collection.find().toArray(function(err, items) {
res.send(items);
db.close();
});
});
});
}
Function called when performing GET on /subscriptions is:
exports.findAll = function(req, res) {
console.log("Get All Subscriptions");
mongo.Db.connect(mongoUri, function (err, db) {
db.collection('subscriptions', function(err, collection) {
collection.find().toArray(function(err, items) {
res.send(items);
db.close();
});
});
});
}
QUESTION: /cronjob/match needs to use BOTH the above functions. Is it best practice to call an Express route from an Express route? Is there a better way to do this without duplicating code all over the place?
Thanks for help!