Node.js 需要express.js控制器oop提示吗
我是web开发的新手,使用node和express开发了一个web服务器。为此,我使用了MVC模式,模型是sequelizejs对象。但是,对于我的控制器,目前几乎没有OOP,我想知道一些编写控制器的OO方法,而不是使用匿名函数来处理请求:Node.js 需要express.js控制器oop提示吗,node.js,oop,express,Node.js,Oop,Express,我是web开发的新手,使用node和express开发了一个web服务器。为此,我使用了MVC模式,模型是sequelizejs对象。但是,对于我的控制器,目前几乎没有OOP,我想知道一些编写控制器的OO方法,而不是使用匿名函数来处理请求: app.get('/test',function(req,res){}) 也许我可以使用URL和模型作为HTTP谓词的属性和方法,为每个路由创建对象: //Use test.model for interacting with model app.get(
app.get('/test',function(req,res){})
也许我可以使用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);
但是,这看起来有点过分了,因为采用这种方式生成的大多数/所有控制器对象最终都是没有继承、多态和重用的单例对象
问:有没有更好的面向对象方法来编写控制器?您可以拥有一个控制器类,其中其构造函数接受express对象,为您设置路由。这是一个示例base
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) {
};
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);
});
};
然后,您可以通过扩展控制器
“类”来创建用户
控制器:
一旦您声明并定义了所有适当的控制器,您就可以开始在express应用程序上实现它们
// Initialize a new instance of your controller.
var usersController = new UsersController(app);
注意:对于构造函数中的快速调用,有另一种方法可以添加创建
,读取
,更新
,删除
路由(以及任何其他路由)。一开始我只是不想把你搞糊涂
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));
};
答案并非如此,但我创建了一个节点模块来处理自动设置快速路线的问题。 然后
以OOP方式组织express的一种方法是使用现有的框架,该框架为此提供了框架。是目前最流行的用于node的MVC框架,使用express.js进行路由。我用TypeScript编写了一个简单的示例,展示了如何为express应用程序编写控制器,并考虑了面向对象编程。它可以很容易地应用于ES6: Server.ts
import * as express from 'express';
import {CategoryController} from 'src/view/CategoryController';
export default class Server {
public app: express.Application;
constructor() {
this.app = express();
this.app.get('/', (request, response) => response.send('Root page without OOP.'));
// Using a controller to serve a view
this.app.use('/categories', CategoryController);
}
}
import {Router, Request, Response} from 'express';
const router: Router = Router();
router.get('/', (request: Request, response: Response): Response => {
const payload: { [index: string]: string[] } = {categories: ['CSS', 'HTML', 'JavaScript', 'TypeScript']};
return response.json(payload);
});
export const CategoryController: Router = router;
类别控制器.ts
import * as express from 'express';
import {CategoryController} from 'src/view/CategoryController';
export default class Server {
public app: express.Application;
constructor() {
this.app = express();
this.app.get('/', (request, response) => response.send('Root page without OOP.'));
// Using a controller to serve a view
this.app.use('/categories', CategoryController);
}
}
import {Router, Request, Response} from 'express';
const router: Router = Router();
router.get('/', (request: Request, response: Response): Response => {
const payload: { [index: string]: string[] } = {categories: ['CSS', 'HTML', 'JavaScript', 'TypeScript']};
return response.json(payload);
});
export const CategoryController: Router = router;
您是否可以添加一个示例,说明如何使用
模块将应用程序
传递给控制器。导出
?