Skip to content

Instantly share code, notes, and snippets.

@floatdrop
Forked from vaseker/test.js
Created July 1, 2014 09:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save floatdrop/aaad77ede3f6fb516a7d to your computer and use it in GitHub Desktop.
Save floatdrop/aaad77ede3f6fb516a7d to your computer and use it in GitHub Desktop.
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);
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