简体   繁体   English

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

[英]How to get full URL in Node.js?

I have a url https://twitter.com/Javeria85685955/status/1059335612346650624 I want to get full url in Node.js on runtime.我有一个 url https://twitter.com/Javeria85685955/status/1059335612346650624我想在运行时在 Node.js 中获取完整的 url。 I am using puppeteer scraping.我正在使用puppeteer抓取。

My Code is :我的代码是

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

It gives me output like :它给了我这样的输出

This is a full URL:这是一个完整的网址:

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

But Required Output I need :但我需要的必需输出

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

If you're using puppeteer then why aren't you taking advantage of the puppeteer API to retrieve your URL as follows:如果您使用的是puppeteer那么为什么不利用puppeteer API 来检索您的 URL,如下所示:

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

Seems like the simplest solution to me?对我来说似乎是最简单的解决方案?

尝试这个:

const myURL = req.headers.referer;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM