node.js - Generic route handlers for express -


i trying make generic handlers rest style routes in express app.

they defined in object merged properties defined in specific route files. merging of properties working fine. issue have in somehow passing model object handler's anonymous function.

the code below clearest attempt showing i'm trying do, fails model lost in anonymous function's scope.

/**  * model mongoose model object  */ routedefinitions: function (resourcename, model) {     routepath = api_prefix + resourcename.tolowercase();      var routeproperties = {         getbyid: {             method: 'get',             isarray: false,             auth: true,             url: routepath + '/:id',             handlers: [function (req, res, next) {                 model.findbyid(req.param('id')).exec(res.handle(function (model) {                     console.log(model);                     res.send(model);                 }));             }]         },         getall: {             method: 'get',             isarray: true,             auth: true,             url: routepath,             handlers: [function (req, res, next) {                  model.find().exec(res.handle(function (model) {                     res.send(model);                 }));                     }]         },         //... (create, update, delete etc)     } } 

i've looked @ few options, , still quite new node/express , connect middleware, there may more obvious missing.

i solved issue calling mongoose.model:

routedefinitions: function (resourcename) {     routepath = api_prefix + resourcename.tolowercase();     var modelname = inflect.singularize(resourcename);     var model = mongoose.model(modelname);      var routeproperties = {         getbyid: {             method: 'get',             isarray: false,             auth: true,             url: routepath + '/:id',             handlers: [function (req, res, next) {                 model.findbyid(req.param('id')).exec(res.handle(function (model) {                     console.log(model);                     res.send(model);                 }));             }]         },         getall: {             method: 'get',             isarray: true,             auth: true,             url: routepath,             handlers: [function (req, res, next) {                  model.find().exec(res.handle(function (model) {                     res.send(model);                 }));                     }]         },         //... (create, update, delete etc)     } } 

Comments

Popular posts from this blog

tcpdump - How to check if server received packet (acknowledged) -