node.js - 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)
});
解决方案
你不见了
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)
});
推荐阅读
- javascript - PayloadTooLargeError:请求实体太大。如何在 NestJS 中解决它?
- openmp - OpenMP 目标中的循环顺序执行
- python - Matplotlib 和赛璐珞在 Jupyter 笔记本上表现出奇怪的行为
- javascript - JavaScript 计算对象数组中特定值被提及的次数
- git - git for windows ssh 凭证不适用于 gitlab
- outlook - 如何在Outlook中选择更改时永久禁用将项目标记为已读
- typescript - 等待可读流 ES8 TypeScript 的完成,fs
- azure-active-directory - 异常:ComponentSpace.SAML2.Exceptions.SAMLBindingException:消息不是 HTTP POST
- spring-integration - 使用 Spring Integration 和 ActiveMQ Artemis 队列发送消息
- python - 使用 tkinter 缩进使用 tab 命令选择的所有单词