如何在Node.js中提取没有HTTP的完整URL?

问题描述 投票:1回答:2

我想提取除HTTP之外的完整URL。我用过Domainurl

这是我的代码

var url = require('url');
var domain=require('domain.js');

var url_parts = 'http://static01.nyt.com/images/2014/11/17/business/billboardjump/billboardjump-master675.jpg'; 

var website=domain(url.parse(url_parts));
var querystring = (url.parse(url_parts, true)).path;

console.log(website+querystring);

但只有我得到

'nyt.com/images/2014/11/17/business/billboardjump/billboardjump-master675.jpg' 

而不是

'static01.nyt.com/images/2014/11/17/business/billboardjump/billboardjump-master675.jpg' 

谢谢。

node.js url
2个回答
2
投票

只需计算协议长度并将其丢弃:

var u = 'http://static01.nyt.com/images/2014/11/17/business/billboardjump/billboardjump-master675.jpg';
var protocol = url.parse(u).protocol;
console.log(u.slice((protocol + '//').length));

0
投票
const urlString = "https://stackoverflow.com/questions/26987567/how-to-extract-complete-url-without-http-in-node-js"

const [protocol, urlWithoutProtocol] = urlString.split('://');

console.log(protocol, urlWithoutProtocol)
© www.soinside.com 2019 - 2024. All rights reserved.