网址解析问题:排除“+”符号

时间:2018-06-06 11:35:37

标签: javascript node.js urlparse

我有一个网址

viberpa://aafff+a@viber.com?phone_number=vv+9nc3pVg==&user_id=cc+9nc3pVg==

当我尝试使用以下示例代码解析URL

//input code
const url = require("url");
 let uri = 'viberpa://aafff+a@viber.com?phone_number=vv+9nc3pVg==&user_id=cc+9nc3pVg==&token_created_time=2018-05-28T11:31:42.760Z'
 let uri_pasrse = url.parse(uri, true);
 console.log(uri_pasrse.query);

获取查询字符串:

{ phone_number: 'vv 9nc3pVg==',
  user_id: 'cc 9nc3pVg==',
  token_created_time: '2018-05-28T11:31:42.760Z' }

查询字符串被排除在+符号之外。如何使用解析方法获取原始查询字符串或建议任何替代方法?

1 个答案:

答案 0 :(得分:0)

每个查询参数必须先编码。在javascript(本例中为NodeJS)中,您可以使用encodeURIComponent:

Volkswagen Golf, Golf Variant(Estate), Golf Plus, CrossGolf (2006-), e-Golf (2014-)Volkswagen Touran, CrossTouran (2007-), Tiguan (2007-);I.D. electric vehicles based on MEB (planning);SEAT new SUV MQB-A2 platform (2018- planning);Components:press shop, chassis, plastics technology;Vehicle 1,140,000 units /year;Vehicle 760,000 units ( 2012 );Vehicle 807,000 units ( 2013 );Vehicle 836,000 units ( 2014 );Vehicle 815,000 units ( 2015 );Vehicle 809,000 units ( 2016 )

<强>结果:

'use strict';

const url = require('url');
const uri = `viberpa://aafff+a@viber.com?phone_number=${encodeURIComponent('vv+9nc3pVg==')}&user_id=${encodeURIComponent('cc+9nc3pVg==')}&token_created_time=2018-05-28T11:31:42.760Z`;
const uri_parse = url.parse(uri, true);
console.log(uri_parse.query);