如果它在子目录中,如何使用 express 提供静态文件

How to Serving static files with express if it is inside a subdirectory

我无法提供 html 文件,该文件位于 public 文件夹的子目录中。 我的目录结构如下:

public/
      HTML/
           index.html
index.js

我想为根路由提供 index.html。

这行不通:

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

根据 the docs,这应该足够了:

app.use(express.static('public'))

它也给你这样的例子

http://localhost:3000/images/kitten.jpg
http://localhost:3000/css/style.css
http://localhost:3000/js/app.js
http://localhost:3000/images/bg.png
http://localhost:3000/hello.html

它只是解释了如何设置多个 public 目录。无论如何,它在我的项目中对我有用。

也许您需要直接说明app.use(express.static('html'))app.use(express.static('public/html'))。让我知道什么对你有用。