繁体   English   中英

刷新 vue 应用程序在开发和生产中出现“Cannot GET /route”错误,尽管设置

[英]Refreshing vue app gives “Cannot GET /route” error in development and production, despite settings

我正在使用 vue 2.6.10 和节点 v10.16.0 并表达 4.17.1。

在我的vue.config.js我做

const path = require('path');

module.exports = {
    outputDir : path.resolve(__dirname, 'dist'),
    devServer:{
        proxy:{
            '/*':{
                target:'http://localhost:4000'
            }
        }
    }
}

在我的 vue router.js我有

const router =  new Router({
  mode: "history",
  base: process.env.BASE_URL,
  routes: [
  //........

在我的app.js for node 中,根据我在网上红色的内容,我有

var history = require('connect-history-api-fallback');
const staticFileMiddleware = express.static(path.join(__dirname,'quad-client')); 

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

app.use(cors());

app.use(express.static(path.join(__dirname,'public'))); 
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false}));
app.use(express.json());
app.use(express.urlencoded({extended:false})); 

app.use('/vesselperformance', require('./routes/vesselperformance'));
//more routes

const port = process.env.PORT || 4000;
app.listen(port, ()=>{
    console.log('server runs on port ', port);
});

如果我这样做,刷新会在开发中出现Cannot GET /route错误,并且在生产中当我 go 到localhost:4000时我看不到应用程序

我必须像这样编辑节点app.js (删除与connect-history-api-fallback相关的代码)

const app = express();
app.use(cors());

app.use(express.static(path.join(__dirname,'public'))); 
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false}));
app.use(express.json());
app.use(express.urlencoded({extended:false})); 

app.use('/login', require('./routes/login'));
//more routes

if(process.env.NODE_ENV === 'production'){
    //static folder
    app.use(express.static(path.join(__dirname,'public'))); 
    //handle SPA
    app.get(/.*/, (req, res)=>{
        res.sendFile(__dirname + '/public/index.html');
    });
}

const port = process.env.PORT || 4000;
app.listen(port, ()=>{
    console.log('server runs on port ', port);
});

这将在localhost:4000中显示应用程序,但在生产和开发中仍然存在刷新问题。

我错过了什么? 如何解决刷新问题? 谢谢。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM