Rohit Rai Rohit Rai - 23 days ago 10
Node.js Question

Reuse Nodejs Api

I have a written an api in nodejs to find list of leads or customers. Now i have to use this api from another controller. How to pass query paramters from another controller and get list of leads from it. Hence will be able to reuse code.

exports.listofLeads = function (req, res) {

var param = req.query.from; var s = "initialSource"; var queryLeads = Customers.find({"attributes": { $size: 0 }} ,{"email":1}); if(! param) {
queryLeads.exec(function (err, articles) {
if (err) {
return res.status(422).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.json(articles);
}
}); } else {
queryLeads.and([ { [s]: param } ]).exec(function (err, articles) {
if (err) {
return res.status(422).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.json(articles);
}
});
}
};

ABC ABC
Answer

Using require() you can access file in which you have written this function. So in other api just call this function and pass parameters as you did in first api