我希望能够为/ stuff和/ stuff / foo等不同路径发送相同的文件,问题是当我尝试这样做时,我收到以下错误:
Refused to execute script from 'http://localhost:8000/stuff/main.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
以下是代码:
app.use("/", express.static(path.join(__dirname, "./../public")));
app.get("/stuff", sendIndexHTML);
app.get("/stuff/:id", sendIndexHTML);
function sendIndexHTML(req, res) {
res.sendFile(path.join(__dirname + "./../public/index.html"));
}
答案 0 :(得分:2)
您在main.js
中引用index.html
作为相对路径,以便从/stuff
获取服务,该路径设置为返回{{ 1}}(其mime类型为index.html
)。
您需要做的是更新html文件中的脚本文件路径。
变化
text/html
到
<script src="main.js"></script>
通过这种方式从配置为提供静态资产的根路径提供服务。