Vue Node.js / express app错误无法设置undefined的属性渲染

问题描述 投票:1回答:1

我有一个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 enter image description here的截图

这些是我在控制台enter image description here得到的任何帮助的错误日志?!

node.js express vue.js
1个回答
0
投票

你错过了

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)
});
© www.soinside.com 2019 - 2024. All rights reserved.