简体   繁体   English

了解Express.js中间件优先级

[英]Understanding Express.js middleware precedence

I'm reading the two examples in the Express.js API reference but I don't get them. 我正在阅读Express.js API参考中的两个示例,但没有得到。

Example No. 1 示例1

Now suppose you wanted to ignore logging requests for static files, but continue logging routes and middleware defined after logger(). 现在假设您要忽略记录静态文件的请求,但是继续记录在logger()之后定义的路由和中间件。 You could simply move static() above it: 您可以将static()移到其上方:

app.use(express.static(__dirname + '/public'));
app.use(logger());
// other middleware

How does this cause requests for static files not to be logged? 这如何导致不记录对静态文件的请求? Isn't all middleware executed (in sequence) for every request? 是否不是针对每个请求都按顺序执行了所有中间件?

Example No. 2 示例2

Another concrete example would be serving files from multiple directories, giving precedence to "./public" over the others: 另一个具体示例是从多个目录提供文件,将“ ./public”优先于其他目录:

app.use(express.static(__dirname + '/public'));
app.use(express.static(__dirname + '/files'));
app.use(express.static(__dirname + '/uploads'));

I suspect that for eg a request like "/js/stuff.js", the first middleware checks if "/public/js/stuff.js" exists. 我怀疑对于例如“ /js/stuff.js”之类的请求,第一个中间件检查“ /public/js/stuff.js”是否存在。 If it does, this middleware handles the request and none of the subsequent middleware is executed (sort-of like an early return). 如果是这样,则此中间件将处理该请求,并且不会执行任何后续中间件 (类似于早期返回)。 If this path however doesn't exist, this middleware passes the request to the next middleware in line. 但是,如果此路径不存在,则此中间件将请求传递给行中的下一个中间件。 Is this correct? 这个对吗?

Please explain both examples! 请解释两个例子!

Express routes are just a collection of middleware. 快速路由只是中间件的集合。 Every request to the server is passed along the middleware chain. 对服务器的每个请求都沿着中间件链传递。

A middleware function has the signature 中间件功能具有签名

function(req, res, next) { }

You can add middleware to the chain with app.use() , as you've seen above. 如上所见,您可以使用app.use()将中间件添加到链中。

Each middleware has two choices. 每个中间件都有两个选择。 It can 它可以

  • Pass the request to the next middleware in the chain, or 将请求传递到链中的下一个中间件,或者
  • End the request chain and send a response 结束请求链并发送响应

If a middleware fails to do either of these, you'll see your request time out and just "spin" endlessly. 如果中间件无法执行上述任何一项操作,您将看到请求超时,并且不断地“旋转”。

To pass a request along, the middleware must call next() . 传递请求,中间件必须调用next() The third argument passed to a middleware is this next() function. 传递给中间件的第三个参数是此next()函数。 It might have a side effect, like the logger middleware above. 可能会有副作用,例如上面的记录器中间件。

To end a request, the middleware can use one of several methods attached to the res object, such as res.send() or res.end() to send a response back to the requestor. 为了结束请求,中间件可以使用附加到res对象的几种方法之一,例如res.send()res.end() ,将响应发送回请求者。 So the express.static middleware has the behavior that, if it finds the requested file, it ends the request chain and sends the file. 因此, express.static中间件具有以下行为:如果找到了所请求的文件,它将结束请求链并发送文件。 If it doesn't find the requested file, it passes the request to the next middleware. 如果找不到请求的文件,它将请求传递给下一个中间件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM