Vue 历史模式和快速服务器收到 404 错误

Car*_*ero 3 express vue.js

我在快速服务器中运行 vue SPA。问题是当使用历史模式并刷新页面时,我收到 404 not found 异常。我尝试使用connect-history-api-fallback但不起作用。

我的快速配置如下所示:

// server.js
var express = require('express');
var history = require('connect-history-api-fallback');


var path = require('path');
var serveStatic = require('serve-static');
app = express();
app.use(serveStatic(__dirname + "/dist"));
var port = process.env.PORT || 5000;
app.use(history())
app.listen(port);
Run Code Online (Sandbox Code Playgroud)

Vla*_*nko 5

您尝试使用带有错误快速服务器脚本的路由器历史模式。为了让事情工作,你可以使用我的脚本。这是我的 server.js,用于在历史模式下运行 Vue 应用程序。server.js(使用 Express):

const express = require('express');
const path = require('path');
const history = require('connect-history-api-fallback');

const app = express();

const staticFileMiddleware = express.static(path.join(__dirname + '/dist'));

app.use(staticFileMiddleware);
app.use(history({
  disableDotRule: true,
  verbose: true
}));
app.use(staticFileMiddleware);

app.get('/', function (req, res) {
  res.render(path.join(__dirname + '/dist/index.html'));
});

var server = app.listen(process.env.PORT || 8080, function () {
  var port = server.address().port;
  console.log("App now running on port", port);
});
Run Code Online (Sandbox Code Playgroud)

将此文件放在项目的根目录中(不是 src)。

使用 Node 运行此脚本: node server.js

不要忘记为生产构建您的应用程序;)!