简体   繁体   English

fallthrough 如何与 express.static() 一起工作?

[英]How does fallthrough work with express.static()?

So, I've a basic express setup as shown below:所以,我有一个基本的快速设置,如下所示:

const path = require("path");
const express = require("express");

const app = express();

app.use(express.static(path.join(__dirname, "public")));

app.get("/", (req, res) => {
  res.send("Home Page");
});

app.get("/about", (req, res) => {
  res.send("About Page");
});

app.listen(3000, () => {
  console.log("Server listening on PORT 3000");
});

In the code above app.use() middleware will be executed for every request because the default path for app.use() is / .在上面的代码中, app.use()中间件将为每个请求执行,因为app.use()的默认路径是/

Now express.static() will be serving the public directory, so if I go to / route I will see the contents of the index.html file and the app.get("/") middleware will not get executed.现在express.static()将服务于public目录,所以如果我 go 到/路由,我将看到index.html文件的内容,并且app.get("/")中间件不会被执行。

But when I go to /about , I see the contents sent from app.get("/about") .但是当我 go 到/about时,我看到了从app.get("/about")发送的内容。 This is what I don't understand because according to docs it calls next() only when the file is not found, but when it is found the request stops there.这是我不明白的,因为根据文档,它仅在找不到文件时才调用next() ,但是当找到文件时,请求就会停止。

So, when I navigate to /about , app.use() will be the first to get executed and it will find index.html file and should render that, but instead it calls next and the get handler for about gets executed.因此,当我导航到/about时, app.use()将是第一个被执行的,它会找到index.html文件并应该呈现它,但它会调用next并执行 about 的get处理程序。 WHY?为什么?

I am not very clear how static assets are being served, I guess when I go to /about it is not actually looking for index.html file but then what file is it looking for?我不太清楚 static 资产是如何提供服务的,我想当我将 go 转到/about时,它实际上并不是在寻找index.html文件,但它在寻找什么文件?

From the express.static() documentation https://expressjs.com/en/4x/api.html#express.static :express.static()文档https://expressjs.com/en/4x/api.html#express.static

The function determines the file to serve by combining req.url with the provided root directory. function 通过将 req.url 与提供的根目录组合来确定要服务的文件。

Optionally, it will also look for a directory index file - see the index option. (可选)它还将查找目录索引文件 - 请参阅index选项。

So in your scenario:所以在你的场景中:

  • / will match the index.html file in your root directory. /将匹配根目录中的index.html文件。
  • /about will try to match a file named about , or an index.html file in the ./about subdirectory. /about将尝试匹配名为about的文件,或./about子目录中的index.html文件。 If there is no such match, then the request will be passed on to the next middelware in line.如果没有这样的匹配,那么请求将被传递到下一个 middelware。

The root argument specifies the root directory from which to serve static assets. root 参数指定提供 static 资产的根目录。 The function determines the file to serve by combining req.url with the provided root directory. function 通过将 req.url 与提供的根目录组合来确定要服务的文件。

So, app.use(express.static(path.join(__dirname, "public"))) serves the public directory.因此, app.use(express.static(path.join(__dirname, "public")))服务于public目录。

Visiting / route参观/路线

Remember the file to serve will be determined by combining req.url with root.请记住,要服务的文件将通过将req.url与 root 组合来确定。 So, in this case req.url is / and root is public .因此,在这种情况下req.url/并且 root 是public So express will try to serve public/index.html , where index.html is the default, if no file is specified explicitly.因此 express 将尝试提供public/index.html ,其中index.html是默认值,如果没有明确指定文件。

Sends the specified directory index file.发送指定的目录索引文件。 Set to false to disable directory indexing.设置为 false 以禁用目录索引。

This file will be found and will be rendered and the request ends there, so the get handler for / is not executed.这个文件将被找到并被渲染并且请求在那里结束,因此/的 get 处理程序不会被执行。

You can set the index property to false and then express will not look for index.html file by default.您可以将index属性设置为false ,然后 express 默认不会查找index.html文件。

So, setting app.use(express.static(path.join(__dirname, "public"), { index: false })) will make express to not look for index.html file by default and now if you visit / route, the get handler for / will be executed.因此,设置app.use(express.static(path.join(__dirname, "public"), { index: false }))将使 express 默认不查找index.html文件,现在如果您访问/路由, /get处理程序将被执行。

Visiting /about route参观/about路线

This time req.url is /about , so express will try to serve public/about/index.html , which will not be found and therefore it calls next() and the get handler for /about gets executed.这次req.url/about ,所以 express 将尝试提供public/about/index.html ,它不会被找到,因此它调用next()并且/about的 get 处理程序被执行。

Visiting /about.html route访问/about.html路线

This time req.url is /about.html , so express will try to serve public/about.html which if found will be rendered else next() will be called.这次req.url/about.html ,所以 express 将尝试提供public/about.html ,如果找到将被称为next()

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

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