方法1:

  1. const http = require('http');
  2. const fs = require('fs');
  3. var express = require('express');
  4. var app = express();
  5. app.set('roots', __dirname+'/dist');
  6. app.use('/', express.static(app.get('roots')));
  7. app.use('*', function (req, res, next) {
  8. fs.readFile(app.get('roots')+'/index.html', 'utf-8', (err, content) => {
  9. if (err) {
  10. console.log('We cannot open "index.htm" file.')
  11. }
  12. res.writeHead(200, {
  13. 'Content-Type': 'text/html; charset=utf-8'
  14. })
  15. res.end(content)
  16. })
  17. });
  18. http.createServer(app).listen(3030,() => {
  19. console.log('Server listening on: http://localhost:%s', 3030)
  20. });

参考文档:vue HTML5 History 模式 node服务器端的配置

方法2:node 服务器对history模式的支持

使用插件 connect-history-api-fallback
参考文档:https://www.yuque.com/u21575953/ya6syy/amdxmm