-
-
Save floatdrop/aaad77ede3f6fb516a7d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Controller = function (name) { | |
if (!name) { | |
return new Error('Controller name is not defined!'); | |
} | |
var controller = this.controller = require('./' + name); | |
Object.keys(controller).forEach(function (method) { | |
this.setMethod(method); | |
}.bind(this)); | |
return this; | |
}; | |
Controller.prototype.setMethod = function (method) { | |
this[method] = function (req, res) { | |
this.controller[method](req, res, function (err, data) {//возможно ли передавать в эту функцию req, res другим способом? | |
if (err) { | |
res.code(500).send(err); | |
} | |
return res.send(data); | |
}); | |
}.bind(this); | |
}; | |
//USAGE | |
var ctl = new Controller('test'); | |
app.get('/test', ctl.list); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var model = require('../models/test'); | |
exports.list = function (req, res, done) { | |
model.list(req.params, req.query).then( | |
function (data) { | |
done(null, data); | |
}, | |
function (err) { | |
done(err); | |
} | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment