[英]Does app.use(express.static("public")) call the middleware for every request?
Does using app.use(express.static("public"))
call the middleware for every request, even if it wasn't a request for a static resource?使用
app.use(express.static("public"))
是否为每个请求调用中间件,即使它不是对 static 资源的请求?
When registering it like that it, the middleware will run on every request, yes.像那样注册时,中间件将在每个请求上运行,是的。 Basically because that statement is actually the same as:
基本上是因为该语句实际上与以下内容相同:
app.use("/", express.static("public"))
Calling express.static
returns a classic middleware function that will be run on every path you specify in app.use
.调用
express.static
会返回一个经典的中间件 function,它将在您在app.use
中指定的每个路径上运行。 If you want it only to kick in on a specific path, you could register it like this:如果你想让它只在特定路径上启动,你可以像这样注册它:
app.use('/static', express.static('public'));
It will only get called if a route hasn't dealt with the request already.只有在路由尚未处理请求时才会调用它。
Keeping in mind that routes are tested in the order they are registered, take this example:请记住,路由是按照它们注册的顺序进行测试的,举个例子:
const express = require('express');
const app = express();
const port = 3000;
app.get('/foo', (req, res) => {
console.log('Foo!');
res.send('Foo!');
});
app.use(function (req, res, next) {
console.log('middleware triggered');
next();
});
app.get('/bar', (req, res) => {
console.log('Bar!');
res.send('Bar!');
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});
If I request http://localhost:3000/foo
then the server will log:如果我请求
http://localhost:3000/foo
然后服务器将记录:
Foo!
The /foo
endpoint matched the request and then called res.send()
. /foo
端点匹配请求,然后调用res.send()
。
If I request http://localhost:3000/bar
then it logs:如果我请求
http://localhost:3000/bar
然后它记录:
middleware triggered
Bar!
The middleware kicks in (because it matches the route), called next()
to go to the next function that matches the route, and then the /bar
handler is called.中间件启动(因为它匹配路由),调用
next()
到 go 到下一个匹配路由的 function,然后调用/bar
处理程序。
It is important to position your static
middleware carefully. position 小心你的
static
中间件很重要。
If you put it before the route you want to match the request then there are two possible negative effects:如果你把它放在你想要匹配请求的路由之前,那么有两种可能的负面影响:
On the other hand, if you put it last then you'll solve the efficiency problem, but some bad route design might mean that something creates a URL which matches an already existing static file and masks it.另一方面,如果你把它放在最后,那么你会解决效率问题,但一些糟糕的路由设计可能意味着某些东西会创建一个 URL 匹配一个已经存在的 static 文件并屏蔽它。
It's a good idea to specify a directory that you know will never conflict with a route (eg app.use('/static', express.static('public'));
) to avoid that possibility.最好指定一个您知道永远不会与路由冲突的目录(例如
app.use('/static', express.static('public'));
)以避免这种可能性。 As a bonus it means that any broken links which would normally 404 won't have to go through the static middleware unless the link is pointing in the /static
path in the first place.作为奖励,这意味着任何通常为 404 的损坏链接都不必通过 static 中间件到 go,除非链接首先指向
/static
路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.