【问题标题】:Vue Node.js/express app error cannot set property render of undefinedVue Node.js/express 应用程序错误无法设置未定义的属性渲染
【发布时间】:2019-02-20 11:59:23
【问题描述】:

我有一个 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 server.js 时,错误会记录到浏览器的控制台中,所以我希望应用程序在给定的浏览器中呈现端口,实际上发生的是,就像我说的,`cannot set property render of undefined
  • 在错误中会有文件名和行号,因此您可以准确地看到错误发生的时间点
  • 再次查看帖子,我添加了错误截图,提前感谢
  • 那么你的错误发生在执行的浏览器部分,所以在你的客户端javascript代码的某个地方;您在此处发送的代码来自您的服务器端应用程序

标签: node.js express vue.js


【解决方案1】:

你不见了

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)
});

【讨论】:

  • 但是 index.html 现在在我的/dist 而不是我的/src 文件夹中,这又是一个 CLI3 设置,但我还是明白了,所以你的意思是如果我添加了 @987654325 @ 指定 index.html 的路径,它将被修复?
  • @Swilam Muhammad 我使用 express 是基于我建议你。你可以试一试,否则我们将删除答案
  • 你不需要说“否则我们将删除答案”我没说错人,我用我的 dist dolder 中的 index.html 的路径尝试了你的答案,但它没有不行。
猜你喜欢
  • 1970-01-01
  • 2018-12-03
  • 2021-11-09
  • 2021-02-17
  • 2020-04-08
  • 2020-12-15
  • 2018-07-14
  • 2021-03-21
  • 2021-08-18
相关资源
最近更新 更多