【问题标题】:unable to serve static vuejs files on express无法在 express 上提供静态 vuejs 文件
【发布时间】:2022-08-19 23:49:59
【问题描述】:

这是我的快速代码:

const express = require(\'express\');
const serveStatic = require(\'serve-static\');
const path = require(\'path\');

// create the express app
const app = express();
var cors = require(\'cors\');
app.use(cors());

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

app.listen(port, () => {
  console.log(\"listening on \"+port)
});

以上代码仅适用于文件夹/dist。但是当我转到/static 时,它会在控制台中显示一个空白页和这个错误:

如果我将 js 文件从 /dist23 放入 /dist,那么 /static 可以工作并向我展示应用程序。它几乎就像是在 /dist 而不是 /dist23 中寻找文件。我如何解决它?

这两个应用程序都是使用vue-2.6.11 构建的。这两个目录都有为生产而构建/捆绑的文件。

    标签: vue.js express


    【解决方案1】:

    您需要在提供静态文件时设置内容类型。

    app.get('/index.html', (req, res) => {
       res.set('content-type', 'text/plain').sendFile('index.html', { root: path.join(__dirname, 'public/dist/') })
    });
    app.get('/', (req, res) => {
       res.set('content-type', 'text/plain').sendFile('index.html', { root: path.join(__dirname, 'public/dist/') })
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多