【发布时间】:2021-01-19 07:50:13
【问题描述】:
我正在尝试将 Node.js 应用程序部署到 Heroku。该应用程序的前端是用 Vue.js 编写的。 Vue 构建输出位于 Node 应用程序中的 /public 下。从localhost 启动时它可以完美运行,并且可以作为https://localhost:8080/public 访问。
当然,它不在 Heroku 上。 X-Content-Type-Options 标头由他们的服务器设置,阻止所有 CSS 和 JS 文件,我在 Chrome 中收到此错误消息:
Refused to apply style from 'https://my-awesome-app.herokuapp.com/css/app.5364ed87.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
我对我的静态文件进行了一些修改,发现了问题所在。在生产版本中index.html 包含这样的链接:
<link href=/js/chunk-109f12d9.3bbd3c2d.js rel=prefetch>
<link href=/js/chunk-2d0bae5c.0ec2a734.js rel=prefetch>
<link href=/js/chunk-2d0f1192.aa2d5399.js rel=prefetch>
<link href=/js/chunk-f8fb7b5a.9218edff.js rel=prefetch>
<link href=/css/app.5364ed87.css rel=preload as=style>
<link href=/js/app.af503387.js rel=preload as=script>
<link href=/js/chunk-vendors.aa1329d3.js rel=preload as=script>
<link href=/css/app.5364ed87.css rel=stylesheet>
我发现如果我从href 属性中删除开头的斜线,链接会突然开始工作。所以css/whatever.css 有效,/css/whatever.css 无效。
我怀疑我在 Express 设置中启用静态目录的方式有问题。但我想不通是什么。你有什么想法吗?
const publicPath = path.join(__dirname, 'public');
this.express.use('/public', express.static(publicPath));
在任何地方添加斜线或删除它都没有帮助。我已经尝试了所有可能的组合。它不适用于/public、/public/、public/、public 等。
难道基础 URL 必须在 Vue 中设置?目前是/。
是的,我知道对此有很多问题,但没有有效的答案。
【问题讨论】:
标签: node.js express vue.js heroku