【问题标题】:How to server static Vue dist folder in express with app.use('someRoute')如何使用 app.use('someRoute') 在 express 中服务静态 Vue dist 文件夹
【发布时间】:2020-01-27 16:09:40
【问题描述】:

我正在从一个 vue 应用程序(使用 create-vue-app 创建)提供一个 dist 文件夹。但是,当我不指向某些特定路线时,它会很好地发挥作用,只需使用根路线而无需任何东西。但是,如果我指定子路由,例如 '/static' ,它不会为 vue app dist index.html 提供服务...我在浏览器中收到消息:

Refused to apply style from 'http://localhost:3000/css/app.fc1b515e.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
4localhost/:1 GET http://localhost:3000/js/app.6869d12b.js net::ERR_ABORTED 404 (Not Found)

但是,如果我将 favicon.ico 输入到浏览器的路径中,它会找到它...

有时,我还会收到错误消息,告诉我 javascript 未启用(这是错误的)...

这就是我设置路线的方式:

  app.use('/static',express.static(path.join(__dirname, '\\client\\dist')));

【问题讨论】:

  • 你在使用 vue-router 吗?

标签: node.js express vue.js routing static


【解决方案1】:

您的 Vue 应用正在引用相对于 / 的资产,如下所示:

/css/app.fc1b515e.css
/js/app.6869d12b.js

...但是您正在为/static 提供资产:

/static/css/app.fc1b515e.css
/static/js/app.6869d12b.js

您可以在 vue.config.js 中将 publicPath 配置选项设置为 /static/,以确保您的 Vue 应用正确引用静态资源。

文档:
https://cli.vuejs.org/config/#publicpath

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-08
    • 1970-01-01
    • 2016-02-02
    • 1970-01-01
    • 2021-04-24
    • 1970-01-01
    • 2021-02-18
    • 2015-11-22
    相关资源
    最近更新 更多