Last active
August 29, 2015 14:03
-
-
Save vaseker/d1b576b44ab41f41bf56 to your computer and use it in GitHub Desktop.
Wrapper для express контроллеров
This file contains hidden or 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 hidden or 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