简体   繁体   中英

NodeJS routing is relative or root-relative depending on user agent

I have a NodeJS server with this routing:

require('log-timestamp');

router.get('/home', async (req, res) => {
  console.log(req.originalUrl, " :: ", req.get('User-Agent'));
  console.log("Home: /home");
  res.render('home');
});

router.get("//home", (req, res) => {
  console.log("Buggy: //home");
  res.redirect("/home");
});

router.get("/", async (req, res) => {
  console.log(req.originalUrl, " :: ", req.get('User-Agent'));
  console.log("Here: /");
  res.redirect("/home");
});

The behavior is different depending on the user agent. If I share the root of the website on Whatsapp, the logs show that NodeJS compounds the routes and makes them relative to the previous one ( / , then //home , then //home/home , then 404 ):

[2020-12-19T13:18:51.159Z] /  ::  WhatsApp/2.2049.10 N
[2020-12-19T13:18:51.161Z] Here: /
GET / 302 7.251 ms - 27
[2020-12-19T13:18:51.412Z] Buggy: //home
GET //home 302 1.731 ms - 27
[2020-12-19T13:18:51.656Z] //home/home  ::  WhatsApp/2.2049.10 N
GET //home/home 404 435.474 ms - 6592
GET /images/logo_400x400_whatsapp_shared_image_EN.png 200 2.909 ms - 36671

If I open the root of the website in a browser, the logs show the logs show that NodeJS uses each route relative to the site root ( / , then /home ):

[2020-12-19T13:22:12.745Z] /  ::  Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:84.0) Gecko/20100101 Firefox/84.0
[2020-12-19T13:22:12.746Z] Here: /
GET / 302 1.476 ms - 54
[2020-12-19T13:22:12.893Z] /home  ::  Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:84.0) Gecko/20100101 Firefox/84.0
[2020-12-19T13:22:12.927Z] Home: /home

The behavior is not critical because Whatsapp only requires the sharing image, which a 404 page does include.

Why does it behave like this? And how can I fix it?

Browsers include ways of converting relative URLs like /home or ./img/logo.png to their absolute equivalents. Like https://example.com/home or https://example.com/home/img/logo.png . That relative-to-absolute conversion is based on the page's Base URL . The default Base URL is document.href , the absolute URL of the page being displayed.

The browser makes requests from absolute URLs. Look at the Network tab of devtools to see how this works. Browsers can do this because they interpret hyperlinks from within the context of the page they're presenting.

Hyperlinks embedded elsewhere don't have that kind of Base URL context available to them. So relative URLs can't be converted to absolute form. So, URLs embedded in non-browser user agents must all be absolute. Your web server (nodejs program) can't fix them.

Handling redirects is also a function of browsers . Non-browser user agents like WhatsApp don't always have the necessary code to accept and apply redirects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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