0

const URL= require("url").Url;
const URLSearchParams= require("url");
var http=require("http");
var url_string = "http://www.example.com/t.html?a=1&b=3&c=m2-m3-m4-m5";
 var url = new URL (url_string);
var c = url.searchParams.get("a");
console.log(c);

Hi I am new to javascript, I have gone through the answer for

How to get the value from the GET parameters?

I have run the above code but iam getting

Type error: cannot read property get of undefined.

I was not able to find the answer anywhere. Can someone please help me and tell me what my mistake is?

Muhammad Shaharyar
  • 1,044
  • 1
  • 8
  • 23
nuy5
  • 27
  • 1
  • 8

2 Answers2

0

From the documentation of 'url' , it looks like the URL object should be upper case:

const URL= require("url").URL;
matmiz
  • 70
  • 9
0

In your code, you use :

const URL= require("url").Url;

NodeJS documentation for the URL module says :

const { URL } = require('url'); // No .Url and with { }

const myURL = new URL('https://example.org/?abc=123');
console.log(myURL.searchParams.get('abc'));

Edit

const { URL } is called destructuring assignment.

This code works for me, tested in Node v7.9 :

const { URL } = require("url");
const URLSearchParams= require("url");
var url_string = "http://www.example.com/t.html?a=1&b=3&c=m2-m3-m4-m5";
var url = new URL (url_string);
var c = url.searchParams.get("a");
console.log(c); // logs : 1
Jeremy Thille
  • 26,047
  • 12
  • 43
  • 63