具有express.js的静态文件 [英] static files with express.js
问题描述
我想将 index.html
和 / media
子目录作为静态文件。索引文件应在 /index.html
和 /
URL之间提供。
I want to serve index.html
and /media
subdirectory as static files. The index file should be served both at /index.html
and /
URLs.
我有
web_server.use("/media", express.static(__dirname + '/media'));
web_server.use("/", express.static(__dirname));
但第二行显然服务于整个 __ dirname
,包括其中的所有文件(不仅仅是 index.html
和 media
),我不想要。
but the second line apparently serves the entire __dirname
, including all files in it (not just index.html
and media
), which I don't want.
我也尝试过
web_server.use("/", express.static(__dirname + '/index.html'));
但访问基本URL /
然后引导要求 web_server / index.html / index.html
(double index.html
组件),当然
but accessing the base URL /
then leads to a request to web_server/index.html/index.html
(double index.html
component), which of course fails.
任何想法?
顺便说一句,我可以在Express这个话题( static()
+它的参数)中找不到任何文档)...令人沮丧。
By the way, I could find absolutely no documentation in Express on this topic (static()
+ its params)... frustrating. A doc link is also welcome.
推荐答案
express.static()
期望第一个参数是路径,而不是文件名。我建议创建另一个子目录来包含你的 index.html
并使用它。
express.static()
expects the first parameter to be a path, not a filename. I would suggest creating another subdirectory to contain your index.html
and use that.
在Express < a href =https://expressjs.com/en/starter/static-files.html =noreferrer>文档。
这篇关于具有express.js的静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!