get url after “#” in express.js middleware request

2019-06-15 01:58发布

I need to get url on server middleware (using express.js). I use req.url but when url starts from /#some/url req.url returns /...

The same with req.path..

Is there a way to get url after # in express.js?

2条回答
Melony?
2楼-- · 2019-06-15 02:26

No. The part of the URL starting with the # symbol is never sent to the server.

The # symbol in an URL is to introduce fragment identifier. This is used to link to a specific part of the page. If a browser loads /#some/url, it will effectively load /, and skip to the HTML element with id="some/url" (if present). The fragment identifier is only relevant to the browser, so it is not sent with the HTTP request.

What you however can do, is using client side Javascript to read out the value of window.location.hash and send it to the server using an XMLHttpRequest. (See other Stack Overflow post.)

查看更多
Ridiculous、
3楼-- · 2019-06-15 02:46

Use the built-in url module which is available in Node.js to parse the URL and create a urlObject. This will have a hash fragment property containing the hash fragment you want.

const url = require('url')
const urlObj = url.parse(req.url)
console.log(urlObj.hash) // #some/url
查看更多
登录 后发表回答