我有一个要求,我要检查URL是否具有绝对路径。
例如:说我有一个网址,例如-https://support.mydomain.com/knowledge-base/abc.html
如果用户在浏览器中最多输入https://support.mydomain.com/knowledge-base/
的URL,它将重定向到https://support.mydomain.com/knowledge-base/abc.html
。
我想检查一下并向用户返回错误消息,提示他们他们未指定完整的URL(缺少abc.html
)。
我尝试探索各种节点模块,但是如果URL末尾有文件名,则没有模块可以提供信息。考虑下面的代码示例:
var myurl = require('url-parse')
myparsedurl = myurl('https://support.mydomain.com/knowledge-base/abc.zyz')
console.log(myparsedurl)
产量
{ 'slashes': true,
'protocol': 'https:',
'hash': '',
'query': '',
'pathname': '/knowledge-base/abc.zyz',
'auth': '',
'host': 'support.mydomain.com',
'port': '',
'hostname': 'support.mydomain.com',
'password': '',
'username': '',
'origin': 'support.mydomain.com',
'href': 'https://support.mydomain.com/knowledge-base/abc.zyz' }
请告知。