我有一个 Vue CLI3 应用程序设置,当我使用 npm runserve
运行应用程序时,它工作得很好,但是,在运行 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)
});