简体   繁体   English

app.use(express.static("public")) 是否为每个请求调用中间件?

[英]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:如果你把它放在你想要匹配请求的路由之前,那么有两种可能的负面影响:

  • You'll call it when it isn't needed which is inefficient你会在不需要的时候调用它,这是低效的
  • A static file will match a route instead of an actual route handler static 文件将匹配路由而不是实际的路由处理程序

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.

相关问题 app.use(“ /”,express.static)和app.use(express.static)之间有区别吗? - Is there a difference between app.use(“/”, express.static) and app.use(express.static)? 使用 app.use(express.static('public')) 时如何测试 Express 应用程序; - How to test an Express application when using app.use(express.static('public')); 如何关闭app.use(express.static('/ public')); 快递js - How to turn off app.use(express.static('/public')); Express js 我的 javascript 文件没有链接? 我正在使用: app.use(express.static(__dirname + '/public')); - my javascript file not linking? Iam using: app.use(express.static(__dirname + '/public')); 如果app-是子应用程序,则`app.use(express.static`似乎不起作用 - `app.use(express.static` seems to not working if app - is subapplication 如何配置express.js的app.use(express.static(…)`? - How to configure express.js's `app.use(express.static(…)`? 与 app.use(express.static(...)) 相关的 next() 中间件/路由是什么? - What are the next() middlewares/routes relative to app.use(express.static(…))? Express:将中间件抽象为应用程序使用 - Express: abstracting away middleware for app.use 为什么 http-auth 不适用于 express.static 中间件? - Why http-auth does not work with express.static middleware? Express Router中间件错误(“ app.use()需要中间件功能”) - Express Router middleware error (`app.use() requires a middleware function`)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM