英文:
middleware and stack functions invoker for expressjs and the response object capture
问题
我看到expressjs应用程序有一个stack
的Layer
对象数组。用于传递请求的函数是哪个?
我想知道:
- 当从
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)]
request
和response
对象是在哪里创建的,作为参数
传递?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 whichfunction is invoked first
and how is the stack array functions withmiddlewares
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
andresponse
objects created that is passed asarguments
? - Where is the
response
object callback function forsend
=>onfinish
/onend
functions which I can use to get thesend
/sendfile's
response object into avariable
for aninterceptor
?
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()
这里,然后开始使用层对象来匹配定义的路由。
你实际上不应该深入研究这些内容,除非出于好奇。通过监听已经在req
或res
对象上触发的事件(例如请求完成时)或者通过重写方法来拦截、修改或增强功能,我已经能够使用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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论