我想提取除HTTP之外的完整URL。我用过Domain和url。
这是我的代码
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'
谢谢。
只需计算协议长度并将其丢弃:
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));
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)