Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresing a vue app gives: Cannot GET /path

I have a simple webpack template vue app, and let's say I have a page with

http://localhost:8080/profile

On my local page, I can go from any page to /profile and even once on /profile I can refresh/reload the page and I get no error.

But I deployed my app on heroku and even though I can navigate from any page to any other, but if I am for example on /profile page and I hit refresh i get

Cannot GET /statement

what could be the problem?

like image 438
hidar Avatar asked Jan 16 '18 09:01

hidar


People also ask

How do I force reload page vue?

You can force-reload components by adding :key="$route. fullPath". However, :key="$route. fullPath" only can force-reload the components of the different route but not the components of the same route.

How do I find my vue router path?

We can use this. $router. currentRoute. path property in a vue router component to get the current path.


1 Answers

You trying to use history mode of router without backend. For getting things working, you may use Express JS. Previous answer is not working for me and i write my own server script. Here is the my server.js for running Vue app with history mode. server.js (with 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);
});

Place this file in the root directory of your project (not src).

Run this script with Node: node server.js

Don't forget build your app for production ;)!

like image 157
Vladislav Gritsenko Avatar answered Sep 21 '22 20:09

Vladislav Gritsenko