我正在使用node进行实验,想要获取URL类的一个实例(由于其便捷的属性),就像这样:
const { URL } = require('url');
(...)
http.createServer((request,response) => {
let uri = new URL(request.url);
(...)
}
但它失败了
TypeError [ERR_INVALID_URL]: Invalid URL: /
这很有趣,因为
const url = require('url');
url.parse();
工作,所以我对它很好奇。我理解这种后来的方法比较旧。
我在本地开发,所以为了发送请求,我在浏览器中使用localhost:8000。
如何使用request
中的信息实例化一个新的URL对象?
我已经查过的信息和东西:
node -v
v9.3.0
https://nodejs.org/api/url.html#url_class_url
https://dev59.com/cVcP5IYBdhLWcg3wNnfW
https://stackoverflow.com/questions/45047840/in-node-js-how-to-get-construct-url-string-from-whatwg-url-including-user-and-pa
https://stackoverflow.com/questions/47481784/does-url-parse-protect-against-in-a-url
https://dev59.com/72Qm5IYBdhLWcg3w-jBL