只使用文件名index.html表达静态工作



所以我的静态文件有问题。这是我的代码:

const express = require ('express')
const app = express()
app.listen(4040,() => console.log("on"))
app.use(express.static(__dirname + "/public"))

一切正常。直到我更改公用文件夹中的html文件名如果它是index.html,它就可以工作,但如果它是about.html或其他所有内容,它就不能工作。

我的文件夹是这样的:

📂Main
📂node_modules
📂public
📂assets
style.css
index.html/about.html
main.js
package-lock.json
package.json

路由必须是特定的,"对于";index.html"&quot/关于";对于";关于.html";。如果您从index.html更改为home.html,请确保您的main.js(服务器(也在后面。

const express = require("express");
const path = require("path");
const app = express();
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "public/index.html"));
});
app.get("/about", (req, res) => {
res.sendFile(path.join(__dirname, "public/about.html"));
});
app.listen(4040, () => console.log("on"));

index.html(http://localhost:4040/)

<h1>HOME PAGE</h1>

about.html(http://localhost:4040/about)

<h1>ABOUT PAGE</h1>

嘿,我相信我可能知道发生了什么,但我想澄清一下,你想提供多个静态文件,一个"index.html";以及另一个";关于.html";它只适用于一个叫index.html的?如果是这种情况,这是因为它默认读取index.html文件(如果存在的话(,解决方案可能是将index.html重命名为其他文件,如main.html,看看这是否正确。

最新更新