Vue router on page refresh gets 404

后端 未结 4 1339
慢半拍i
慢半拍i 2021-01-28 02:49

I am using Vue router with history mode. On button click on the current page I route it to next page. On second page when i reload i get a 404. Is there a way to handle this in

4条回答
  •  暖寄归人
    2021-01-28 03:46

    This is related to how the history mode in Vue Router works. I have created a writeup about it, with a solution as well.

    Depends on your server implementation you need to enable URL Rewrites there. Here's how this would be implemented using Express/Node.js:

    const express = require('express');
    const app = express();
    const port = 80;
    const buildLocation = 'dist';
    app.use(express.static(`${buildLocation}`));
    app.use((req, res, next) => {
      if (!req.originalUrl.includes(buildLocation)) {
        res.sendFile(`${__dirname}/${buildLocation}/index.html`);
      } else {
        next();
      }
    });
    app.listen(port, () => console.info(`Server running on port ${port}`));
    

    For more info, check out https://blog.fullstacktraining.com/404-after-refreshing-the-browser-for-angular-vue-js-app/. HTH

提交回复
热议问题