需要express.js控制器oop提示

use*_*008 3 oop node.js express

我是网络开发的新手,并使用node和express开发了一个Web服务器.我已经使用了MVC模式,模型是sequelizejs对象.但是,对于我的控制器,你当前很少甚至没有OOP,我想知道一些OO编写控制器的方式,而不是使用匿名函数来处理请求:

app.get('/test',function(req,res){})
Run Code Online (Sandbox Code Playgroud)

也许我可以使用URL和模型作为HTTP谓词的属性和方法为每个路径创建对象:

//Use test.model for interacting with model
app.get(test.URL,test.get);
app.post(test.URL,test.post);
app.put(test.URL,test.put);
app.patch(test.URL,test.patch);
app.delete(test.URL,test.delete);
Run Code Online (Sandbox Code Playgroud)

但是,它看起来有点过分,因为以这种方式制作的大多数/所有控制器对象最终将成为没有继承,多态和重用的单例.

问题:是否有更好的OO方式来编写控制器?

Sal*_*Sal 15

您可以拥有一个控制器类,其构造函数接受一个快速对象,为您设置路径.所以这是一个示例基Controller类:

/**
 * @param connect can either be Sencha Labs' `connect` module, or
 */
function Controller(express) {
  var self = this;
  var name = '/' + this._name;
  express.post(name, function (req, res, next) {
    self._create(req, res, next);
  });
  express.get(name, function (req, res, next) {
    self._read(req, res, next);
  });
  express.put(name + '/:id', function (req, res, next) {
    self._update(req, res, next);
  });
  express.delete(name + '/:id', function (req, res, next) {
    self._delete(req, res, next);
  });
}

// Since there aren't any protected variables in JavaScript, use
// underscores to tell other programmers that `name` is protected. `name`
// (or, more technically, `_name`) is still accessible, but at least, if a
// team is disciplined enough, they'd know better than to access variables
// with underscores in them.
Controller.prototype._name = '';

Controller.prototype._create = function (req, res, next) {
};

Controller.prototype._read = function (req, res, next) {
};

Controller.protoype._update = function (req, res, next) {
};

Controller.prototype._delete = function (req, res, next) {
};
Run Code Online (Sandbox Code Playgroud)

然后,您可以Users通过从Controller"类" 扩展来创建控制器:

function UsersController(express) {
  Controller.call(this, express);
}

// This is not the most perfect way to implement inheritance in JavaScript,
// this is one of the many ways.
UsersController.prototype = Controller.prototype;

UsersController.prototype._name = 'users'

// An example override of the base `Controller#create` method.
UsersController.prototype._create = function (req, res, next) {
  db.save(req.body, function (err) {
    if (err) res.send(500, err.message);
    res.redirect('/');
  });
};

UsersController.prototype._read = function (req, res, next) {
  db.read(function (err, users) {
    if (err) res.send(500, err.message);
    res.send(users);
  });
};
Run Code Online (Sandbox Code Playgroud)

一旦您声明并定义了所有适当的控制器,您就可以开始在您的快速应用程序上实现它们.

// Initialize a new instance of your controller.
var usersController = new UsersController(app);
Run Code Online (Sandbox Code Playgroud)

PS:对于在构造函数中调用快车,还有另一种方式来增加你的create,read,update,delete路线(以及任何其他途径).我一开始并不想迷惑你.

function Controller(express) {
  var name = '/' + this._name;
  express.post(name, this._create.bind(this));
  express.get(name, this._read.bind(this));
  express.put([name , ':id'].join('/'), this._update.bind(this));
  express.delete([name, ':id'].join('/'), this._delete.bind(this));
};
Run Code Online (Sandbox Code Playgroud)