理解expressjs基本代码 [英] understanding expressjs base code
问题描述
我正在检查 express.js
代码,我试图重写它只是为了学习如何创建中间件(框架)。但是代码周围的所有继承都让我感到困惑。
I am checking the express.js
code and am trying to rewrite it just to learn how to create middlewares (framework). But all the inheritance around the code is confusing me.
相关代码:
- express.js
- application.js
- request.js
- response.js
在 express.js 上此代码:
app.request = { __proto__: req, app: app };
app.response = { __proto__: res, app: app };
保持谨慎 __ proto __
已弃用。 @Bergi告诉我我可以用以下代码替换此类代码:
keeping in-mind __proto__
is deprecated. @Bergi told me that I can replace such code with:
app.request = Object.create(req);
app.request.app = app;
app.response = Object.create(res);
app.response.app = app;
但现在奇怪的是, application.js 又有类似的代码 - 我认为。
But now oddly the application.js again has similar code - i think.
// inherit protos
this.on('mount', function(parent){
this.request.__proto__ = parent.request;
this.response.__proto__ = parent.response;
this.engines.__proto__ = parent.engines;
this.settings.__proto__ = parent.settings;
});
这里发出的是:
mount_app.mountpath = mount_path;
mount_app.parent = this;
// restore .app property on req and res
router.use(mount_path, function mounted_app(req, res, next) {
var orig = req.app;
mount_app.handle(req, res, function(err) {
req.__proto__ = orig.request;
res.__proto__ = orig.response;
next(err);
});
});
// mounted an app
mount_app.emit('mount', this);
然后连 request.js 和 response.js 有继承,似乎可以理解,虽然我不完全理解他们是如何做的。
And then even the request.js and response.js have inheritance, seems understandable, though I don't completely understand how they are doing it.
//is this exporting http.IncomingMessage.prototype???
var req = exports = module.exports = {
__proto__: http.IncomingMessage.prototype
};
我对 javascript
不太满意。我想找一些关于继承主题的书籍。
I am not very good with javascript
. I would like find books to read on the topic of inheritance.
我的问题:
- 所有这些继承的重点是什么。
- 我指出的第一和第二种情况不是多余的吗?
- 我应该如何重写避免弃用的
__ proto __
?
- What is the point of all this inheritance.
- Isn't the 1st and 2nd case I pointed out redundant?
- And how should I rewrite avoiding deprecated
__proto__
?
我实际上只是想编写一个简单的基于中间件的系统。我可以做的事情:
I am actually just trying to write a simple middleware based system. Something I can do:
// route
app.use('/', function(req,res) {
});
就这么简单,但我也想添加更多方法 req
和 res
。这就是为什么我正在研究如何 connect
和 express
实现它。虽然 connect
没有向 req
和 res
。这就是我试图理解 express
的原因。
Just that simple, but I also want to add more methods to req
and res
. That is why I am looking at how connect
and express
implemented it. Though connect
didn't add additional methods to req
and res
. That's why I am trying to understand express
.
向添加方法的一种简单方法res
或 req
是:
// middleware
app.use(function(req, res) {
req.mymethod = function()...
});
现在下一个中间件还有额外的 req
方法,但我发现这很脏。这就是我试图理解 expressjs
以及它们如何实现这种继承的原因。
Now the next middleware has the additional req
method but I find this dirty. That is why I am trying to understand expressjs
and how they implemented that inheritance.
注意:我成功编写了一个工作简单的中间件系统,但仍然不知道如何向req / res添加方法。我也在调查对象包装(也许这就是他们对req和res做了什么?)
Note: I successfully wrote a working simple middleware system but still don't know how to add methods to req/res. I'm also looking into object wrapping (maybe that is what they did to the req and res?)
推荐答案
这种方法没有任何内在错误:
There's nothing inherently wrong with this approach:
app.use(function(req, res) {
req.mymethod = function()...
});
中间件按其定义的顺序运行,只要它出现在您的其他中间件和路由之前,你确信它会在那里。
The middleware runs in the order it's defined, so as long as that appears before your other middleware and routes, you have assurance that it'll be there.
就个人而言,如果它是一个没有做任何花哨的静态功能,我会把它存放在一个单独的模块, require()
我需要的地方,并保留它。但是,如果出于某种原因,您需要特定于请求的变量,那么按照您的建议进行操作可能会很方便。例如,创建一个闭包:
Personally, if it was a static function that wasn't doing anything fancy, I'd store it in a separate module, require()
it where I need it, and leave it at that. But if, for some reason, you need request-specific variables in there, it might be handy to do it as you suggested. For example, creating a closure:
app.use(function(req, res, next) {
// Keep a reference to `someVar` in a closure
req.makeMessage = (function(someVar) {
return function () {
res.json({hello : someVar});
};
})(/* put the var here */);
next();
});
app.get('/hello', function(req, res) {
req.makeMessage();
});
一个人为的,相当无用的例子,但在某些情况下可能会有用。
A contrived, rather useless example, but it could be useful in some cases.
这篇关于理解expressjs基本代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!