首页 > 解决方案 > Vue Node.js/express 应用程序错误无法设置未定义的属性渲染

问题描述

我有一个 Vue CLI3 应用程序设置,当我使用 运行应用程序时它工作得非常好npm run serve,但是,在运行npm run build准备应用程序以进行部署之后,当使用 Express 运行应用程序时,它在控制台中给我一个错误说cannot set property render of undefined..这里是我的应用程序根目录中的 server.js 文件设置

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

let app = express();
app.use(serveStatic(__dirname + "/dist"));


const port = process.env.PORT || 5000;


app.get('*', (req, res) => {
    res.sendFile(path.join( __dirname, './dist/index.html')); //path to index.html
  });

app.listen(port, () => {
  console.log('Listening on port ' + port)
});

这是我的 package.json 的截图 在此处输入图像描述

这些是我在控制台中得到的错误日志,有 在此处输入图像描述 什么帮助吗?!

标签: node.jsexpressvue.js

解决方案


你不见了

app.get('*', function(req, res) {
  res.sendFile(path.join( __dirname, './index.html')); //path to index.html
});

你的新代码就像

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

let app = express();
app.use(serveStatic(__dirname + "/dist"));

const port = process.env.PORT || 5000;
  app.get('*', function(req, res) {
      res.sendFile(path.join( __dirname, './index.html')); //path to index.html
    });


app.listen(port, () => {
  console.log('Listening on port ' + port)
});

推荐阅读