2

So on my website, I have several pages of content. On one of the pages, a forward slash is appended to my address bar even if I point my browser to example.com/guides thus leaving me with example.com/guides/. To be clear, this page loads perfectly fine. When I point my address bar to example.com/about, it leaves the url as is and loads the page. When I visit example.com/about/ it loads the same page and the forward slash is left to be. Here is the code from how my server handles these get requests:

app.get('/about', (req, res) => {
    res.sendFile(__dirname + '/views/about.html');
});
app.get('/guides', (req, res) => {
    res.sendFile(__dirname + '/views/guides.html');
});

As you can see, both html files are rendered the exact same way. I have no JavaScript on either of the two pages, and I am not loading any external libraries on either of the pages. This guides page is the only one with this behavior. There are also no errors in the console or my server logs. Thank you in advance.

EDIT: After examining the network page in my developer console, it returns 301 Moved Permanently (from disk cache) for the GET request status code.

Jonah
  • 21
  • 1
  • 3
  • have tried to open it from incognito mode of browser? maybe browser's cached behaviour – num8er Feb 19 '19 at 13:09
  • 1
    Is this helpful?: https://stackoverflow.com/questions/48362226/express-adds-trailing-slash or this?: https://stackoverflow.com/questions/40124892/trailing-slash-gets-appended-to-url-in-express-server – OliverRadini Feb 19 '19 at 13:09
  • @num8er Tried that, same issue. – Jonah Feb 19 '19 at 13:23
  • @OliverRadini Both links are relevant but neither solutions work. My browser's get request is returning status code 301 and it does seems to be a caching issue. – Jonah Feb 19 '19 at 13:30
  • how You run Your web app? behind some http server (like: nginx, apache)? is it accessible from public to watch what it returns? how about doing: `curl -v http://localhost:3000/about` - want to see headers which it returns. also please provide all app routings and app initialization code. – num8er Feb 19 '19 at 13:55
  • It's run on heroku.com, not localhost. – Jonah Feb 19 '19 at 14:02
  • @Jonah please provide link to it let us check, add full code of app where app is configured, routes been set, listen started – num8er Feb 19 '19 at 14:24
  • The app has over 300 lines, and I do not have a router. I have provided all of the relevant code to the issue. – Jonah Feb 19 '19 at 14:45
  • @Jonah there may be middlewares attached that dynamically redirects to slashed urls. If You don't want to share it's Your problem, but Your question description is not enough to give correct answer. Your issue cannot be reproduced. – num8er Feb 19 '19 at 17:37
  • @num8er I haven't added any middlewares to my app so I don't know how that would be a problem. – Jonah Feb 19 '19 at 19:48
  • @Jonah add me to skype: **anarjafarov** to debug it together – num8er Feb 19 '19 at 20:34
  • @num8er Discord would be better, I don't have skype. – Jonah Feb 20 '19 at 13:45

1 Answers1

0

By default strict routing is disabled so it treats /guides & /guides/ as same, so whatever you have visited one replaces every time. So enable strict routing by this

var router = express.Router({ strict: true });

now it will not treat them as same, and also it will not change /guides to /guides/.

Muhammad Aadil Banaras
  • 1,134
  • 1
  • 11
  • 21