Express 不会在具有多个参数的路由中从公共文件夹加载静态文件

问题描述 投票:0回答:1

我正在尝试渲染页面,但 Express 不会仅在具有单个参数的路由上加载静态文件

静态配置:

app.use(express.static(path.resolve(__dirname, "public")));

路线:

routes.get("/param/:id", (req, res) => res.render("page"));

错误:

Failed to load resource: the server responded with a status of 404 (Not Found)

Refused to execute script from 'http://localhost:port/param/1/assets/js/bundle.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.

我已经尝试了一切;也许这与我的项目结构有关?

回购:项目议程

javascript node.js express routes static
1个回答
0
投票
  1. 将您的
    static
    更改为:
app.use(express.static(path.join(__dirname, '/public')));
  1. 在视图文件中将链接更改为始终以
    /
    开头,例如:
<script src="/assets/js/bundle.js"></script>
© www.soinside.com 2019 - 2024. All rights reserved.