首页 > 解决方案 > npm build build vue via express build,但是 index.html 不起作用

问题描述

前端/config/index.js

  build: {
    index: path.resolve(__dirname, '../../backend/public/index.html'),
    assetsRoot: path.resolve(__dirname, '../../backend/public'),
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',
  }

后端/app.js

...
app.use(require('connect-history-api-fallback')());
...
app.use('/', root);

后端/路由/root.js

  const express = require('express');
  const path = require('path');
  const router = express.Router();

  router.get('/', (req, res, next) =>{
    res.sendFile(path.join(__dirname, '../public/index.html'))
  });

  module.exports = router;

我通过 npm run build 在后端创建了一个公共文件夹。

我通过 node app.js 运行 express 服务器,但我无法访问 index.html 和任何 vue 组件。

当与 localhost: // 3000 连接时,我得到Cannot GET /的不是 index.html。

我怎么了?

标签: node.jsexpressvue.js

解决方案


推荐阅读