javascript 在 express.js 中间件请求中“#”后获取 url
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17744003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
get url after "#" in express.js middleware request
提问by Kosmetika
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 /
...
我需要在服务器中间件上获取 url(使用 express.js)。我使用req.url
但是当 url 从/#some/url
req.url开始返回时/
......
The same with req.path
..
同req.path
..
Is there a way to get url after #
in express.js?
有没有办法#
在 express.js之后获取 url ?
回答by dsprenkels
No.The part of the URL starting with the #
symbol is never sent to the server.
号开始与URL的一部分#
符号永远不会发送到服务器。
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.
#
URL 中的符号是引入片段标识符。这用于链接到页面的特定部分。如果浏览器加载/#some/url
,它将有效地加载/
,并跳转到带有id="some/url"
(如果存在)的 HTML 元素。片段标识符仅与浏览器相关,因此不会与 HTTP 请求一起发送。
What you however cando, 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.)
但是,您可以做的是使用客户端 Javascript 读取 的值window.location.hash
并使用 XMLHttpRequest 将其发送到服务器。(请参阅其他 Stack Overflow 帖子。)
回答by Alex Chesters
Use the built-in url
modulewhich 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.
使用Node.js 中可用的内置url
模块来解析 URL 并创建一个urlObject
. 这将有一个hash
包含您想要的哈希片段的片段属性。
const url = require('url')
const urlObj = url.parse(req.url)
console.log(urlObj.hash) // #some/url