samedi 28 mars 2015

creating a dinamically restful api for node.js

I'm using mongodb for pretty much everything in my node.js application, and now i want create a restful application, so, i did that:


I'm trying to do just the get method, for now:


restApi.js:



var restAPI = {

get: function(method, model, sort, limit, options) {
if (method !== 'get') {
return;
}

model.find(options).sort(sort).limit(3).exec(function (error, result) {
if (error) {
return error;
} else {
return result;
}
});

},
};


And now i can require this in my route:


var restApi = require('restApi');


and use like this:



app.get('/', function(req, res, next) {
var result = restAPI.get('get', Event, 'date', 3, {'isActive': true});

res.render('/', {
result: result
});
});


Is not working, the result is undefined. Why??


How can i transform this in a async function with callback? This is possible?


Thanks! :)


Aucun commentaire:

Enregistrer un commentaire