如何在Node.js中获取完整的URL?

时间:2018-11-05 09:51:24

标签: javascript node.js url puppeteer

我有一个网址https://twitter.com/Javeria85685955/status/1059335612346650624 我想在运行时在Node.js中获得完整的URL。我正在使用puppeteer抓取。

我的代码是

const url = require('url');
const myURL = url.parse(req.url, true);
console.log('This is a full URL: ' + JSON.stringify(myURL))

它给了我类似的输出

这是完整的URL:

{
    "protocol": null,
    "slashes": null,
    "auth": null,
    "host": null,
    "port": null,
    "hostname": null,
    "hash": null,
    "search": null,
    "query": {},
    "pathname": "/",
    "path": "/",
    "href": "/"
}

但我需要必需的输出

https://twitter.com/Javeria85685955/status/1059335612346650624

2 个答案:

答案 0 :(得分:0)

尝试一下:

const myURL = req.headers.referer;

答案 1 :(得分:0)

如果您使用的是puppeteer,那么为什么不利用puppeteer API来按如下方式检索URL:

const url = await page.url();
console.log(url); // This will output the current URL to the console

似乎对我来说是最简单的解决方案?