当我更改我的 index.html 文件的服务方式时,我的路径有哪些不起作用?

What aren't my paths working when I change the way I serve my index.html file?

我最近决定更新 index.html 文件的服务方式

app.use('/', express.static(__dirname + '/..')); 

app.get('/', function(req, res) {
  res.sendFile(path.join(__dirname + '/../index.html'));
});

两者都工作正常,因为它们都能正确地为我的 index.html 文件提供服务。但是,当我切换到使用 app.get ...

我无法再让我的路径在我的 index.html 文件中工作。

例如,我的 index.html 文件中有一个 .svg 文件,其作用如下:

<img src="dist/images/svg/favicon.svg" width="128" height="128">

现在失败的请求(404 错误)在控制台中看起来像这样:

http://localhost:3000/dist/images/svg/favicon.svg

我试过在根目录下添加如下:

<img src="ll-server/dist/images/svg/favicon.svg" width="128" height="128">

但这无济于事 - 都出现 404 错误。

为什么更改我的 index.html 文件服务方式会破坏我在 index.html 文件中的路径?

我该如何解决这个问题?

Why is changing the way I serve my index.html file breaking my paths inside the index.html file?

use 为每个以指定路径开始的 URL 运行一些代码。该代码 (static) 查看 URL,在指定路径上找到匹配的文件,并提供该文件。

get 为特定的 URL 运行一些代码(至少在这种情况下,因为您没有使用 * 或参数)并且该代码提供 index.html 并且只有 index.html.

您的图像已停止工作,因为您删除了提供 HTML 的代码以及仅提供 HTML.

代码的图像

How can I fix this?

将代码恢复原样。您所做的更改不合理。

——-

然后解决您的安全问题。

如果你的 JS 模块所在的目录是父目录,那么你提供静态文件的目录就是父目录。这意味着你已经将你的 JS 模块(可能还有你所有的 JS 模块)URLs 放在你的网络服务器。

不要公开您的服务器端业务逻辑。

更改您的目录结构。一个典型的是:

 /            (containing the project)
 /src/        (containing the JS)
 /static/     (containing the static files)