中间件和堆栈函数调用器用于 Express.js 和响应对象捕获。

huangapple go评论75阅读模式
英文:

middleware and stack functions invoker for expressjs and the response object capture

问题

我看到expressjs应用程序有一个stackLayer对象数组。用于传递请求的函数是哪个?

我想知道:

  • 当从http客户端发送请求时,哪个函数首先被调用,并且中间件如何逐步调用并传递结果?
  • 这里有一个例子 app.get(path, [middleware(req, res, next), m2(req, res, next), m3(req, res, next), m4(req, res, next)], handler(req, res)) => 哪个函数逐步调用函数 => [middleware(req, res, next), m2(req, res, next), m3(req, res, next), m4(req, res, next), handler(req, res)]
  • requestresponse 对象是在哪里创建的,作为参数传递?
  • response 对象的 send 回调函数在哪里 => onfinish/ onend 函数,我可以用它来获取send/ sendfile的响应对象到一个变量中,用于拦截器

我正在寻找请求的入口和响应的出口(带有响应结果)。

英文:

I see that expressjs app has a stack of Layer object Arrays. Which is the function that is used to pass the

I want to know:

  • When a request is sent from the http client which function is invoked first and how is the stack array functions with middlewares invoked and results passed stepwise?
  • Here is an example app.get(path, [middleware(req, res, next), m2(req, res, next), m3(req, res, next), m4(req, res, next)], handler(req, res)) => which function invokes functions stepwise => [middleware(req, res, next), m2(req, res, next), m3(req, res, next), m4(req, res, next), handler(req, res)]
  • Where is the request and response objects created that is passed as arguments?
  • Where is the response object callback function for send => onfinish/ onend functions which I can use to get the send/ sendfile's response object into a variable for an interceptor?

I am looking for the entry of the request and exit if the response (with the response result).

答案1

得分: 1

如果你看一下app.listen()的代码这里,你会看到这个:

app.listen = function listen() {
  var server = http.createServer(this);
  return server.listen.apply(server, arguments);
};

所以,它将app函数对象注册为http.createServer()的回调函数。这将调用app函数,该函数位于这里,用于处理任何传入的http请求,该函数如下所示:

var app = function(req, res, next) {
  app.handle(req, res, next);
};

是的,这是一个有点奇怪的设计,其中app对象实际上是一个函数(因为函数也是对象),但这是Express的做法。

因此,任何路由的起点是app.handle()这里,然后它调用router.handle()这里,然后开始使用层对象来匹配定义的路由。


你实际上不应该深入研究这些内容,除非出于好奇。通过监听已经在reqres对象上触发的事件(例如请求完成时)或者通过重写方法来拦截、修改或增强功能,我已经能够使用Express做各种自定义操作。所有这些都可以从Express源代码以外和私有数据结构之外完成。

这就是我之前要求你描述实际问题的原因,因为很可能你不必深入了解Express的私有数据结构来解决大多数实际问题。公共事件和方法已经非常强大。

英文:

If you look at the code for app.listen() here, you see this:

app.listen = function listen() {
  var server = http.createServer(this);
  return server.listen.apply(server, arguments);
};

So, it is registering the app function object as a callback to http.createServer(). That will call the app function which is here for any incoming http request and that function looks like this:

var app = function(req, res, next) {
  app.handle(req, res, next);
};

Yes, this is a bit of weird design where the app object is actually a function (since functions are objects too), but that's the way Express did it.

So, the start of any routing is app.handle() here, which then ends up calling router.handle() here which then starts working with the layer objects to match defined routes.


You really shouldn't have to dive into any of this for anything other than curiosity. I have been able to do all sorts of custom things with Express by either listening to events that are already being fired on the req or res objects (such as when the request is done) or by overriding methods to intercept or modify or enhance functionality. All of this can be done from outside the source code and the private data structures of Express.

This is why I was asking you to describe your actual problem because there's a high likelihood that you don't have to go underneath the covers of Express into its private data structures to solve most actual problems. The public events and methods are quite capable.

huangapple
  • 本文由 发表于 2023年5月7日 17:24:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76193074.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定