-1

I am trying to parse a URL and I am returning []. How do I format my simplified query to grab the ID from the URL?

const url = require('url');

const apURL = new URL('https://local.apex.json/user/11225');
const search_params = current_url.searchParams;

const id = search_params.get('id');


console.log(id);
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
DAJAZ
  • 1

2 Answers2

0

Are you trying to parse the url? If so, this will work

const url = require('url');

pathString = url.parse('https://local.apex.json/user/11225').path;

console.log('Full path', pathString);

splitPathString = pathString.split('/');

console.log('After split ', splitPathString);

console.log('Get a specific array item post split ', splitPathString[2]);

Console results as below, Full path /user/11225

After split [ '', 'user', '11225' ]

Get a specific array item post split 11225

Sathish
  • 71
  • 4
  • Thank you for all the help. I may have asked the question incorrectly. https://local.apex.json/user/11225 contains a Json object from a database. The items include userID, user_name, user_email, api_name, api_token. I need to grab the api username and api token. My apologies for the confusion. – DAJAZ Jun 04 '20 at 15:12
0

You need to parse de url. Yo do not need to do new URL('https://local.apex.json/user/11225').

You only need to do this:

var url = require('url');
var url_parts = url.parse("https://local.apex.json/user/11225", true);
var query = url_parts.query;

Here is the full answer: https://stackoverflow.com/a/6912872/5854328

D.Pacheco
  • 520
  • 6
  • 24