我正在开发一个Web应用程序,我需要使用不同的路径来提供保存的文件集。
我在文件夹./foo/
中写了一个网站,其中包含index.html
,style.css
和main.js
这3个文件
我希望将./foo/
文件夹中的网站提供以下路径。
http://localhost:8080/foo/a/
http://localhost:8080/foo/b/
http://localhost:8080/foo/c/
因此,以下链接将返回./foo/
文件夹中的相应文件。
http://localhost:8080/foo/a/index.html
http://localhost:8080/foo/a/style.css
http://localhost:8080/foo/a/main.js
http://localhost:8080/foo/b/index.html
http://localhost:8080/foo/b/style.css
http://localhost:8080/foo/b/main.js
http://localhost:8080/foo/c/index.html
http://localhost:8080/foo/c/style.css
http://localhost:8080/foo/c/main.js
以下代码对我有用。 Express提供正确的文件。
app.use("/foo/:id/", express.static("./lib/foo/"));
但是,我不需要id属性,因此我将:id
替换为*
app.use("/foo/*/", express.static("./lib/foo/"));
上面的代码不起作用。无论我请求哪个路径,都请表达./foo/index.html
的返回内容,并在URL中添加尾部斜杠。
例如,当我输入http://localhost:8080/foo/a/style.css
时,服务器应返回./foo/style.css
,但服务器将在URL(http://localhost:8080/foo/a/style.css/
)中插入斜杠,然后返回index.html
,这是错误的文件。
Codesandbox:https://codesandbox.io/s/express-gbnb3
如何修复服务器?