vlio20
vlio20

Reputation: 9295

Express prevent redirect in SPA

I have the following routes configuration:

const app = express();

app.use('/site', express.static(path.join(__dirname, '../../site/static')));
app.use('/site*', express.static(path.join(__dirname, '../../site/static')));

Now when I am accessing the app http://localhost:8080/site/bla?asdad I am being redirected to http://localhost:8080/site/bla/?asdad (note the slash that was added before the question mark).

The application is written with angular (1) and it causes the router (ui-router) to go to the default router.

When navigating within the app (clicking the links to different states) it all works correctly but on refresh the slash is added and redirects to the default route.

I have tried to add {redirect: false} to the static middelware options but it caused this url http://localhost:8080/site/bla/?asdad to return:

Cannot GET /site/bla

What should I do to make it work.

Upvotes: 2

Views: 1177

Answers (1)

vlio20
vlio20

Reputation: 9295

This is how it worked for me:

app.use('/site', express.static(path.join(__dirname, '../../site/static')));

app.use('/site*', (req, res) => {
  res.sendfile(path.join(__dirname, '../../site/static/index.html');
});

Upvotes: 1

Related Questions