How can I allocate URL objects in node.js? - node.js

How can I allocate an instance of a URL object using node.js from an existing url string?
Something like this:
var url = require('url');
var myurl = new url("http://google.com/blah");
I can't seem to find any mention/example of this anywhere.

var url = require('url');
var myurl = url.parse('http://google.com/blah');
You can now use
myurl.hostname // google.com
myurl.pathname // /blah
and so on..
http://nodejs.org/api.html#url-302
You very rarely (if ever) need to use the new keyword in relation to the built-in modules, as long as you use the documented functions.

Related

How can i properly simplify my url using variables?

I'm trying to make an esports web page, but first I'm playing with the API.
the problem is that when I want to simplify my url like this.
var url = "https://api.pandascore.co/lol/champions/2524?token="+accesskey+""
var accesskey = "example"
sends me an 401 error or accesskey invalid but when I code it like this
var url = "https://api.pandascore.co/lol/champions/2524?token="example"
it works, so I don't know if I'm simplifying bad my code :(.
+"" isn't necessary try with just:
var url = "https://api.pandascore.co/lol/champions/2524?token="+accesskey
You can also do it with template literals like this
var url =`https://api.pandascore.co/lol/champions/2524?token=${accesskey}`;

url.searchParams returns undefined in node.js

In the following node.js example:
var url = require('url');
var urlString='/status?name=ryan'
var parseObj= url.parse(urlString);
console.log(urlString);
var params = parseObj.searchParams;
console.log(JSON.stringify(params));
the property searchParams is undefined. I would expect searchParams to contain the parameters of the search query.
As you see in https://nodejs.org/dist/latest-v8.x/docs/api/url.html#url_class_urlsearchparams
searchParams is a proxy to an URL object. You must obtain a new URL complete object (with domain and protocol) and then you can use searchParams:
var url = require('url');
var urlString='https://this.com/status?name=ryan'
var parseObj= new url.URL(urlString);
console.log(urlString);
var params = parseObj.searchParams;
console.log(params);
Other way is using the query attribute (you must pass true as second parameter to url.parse):
var urlString='/status?name=ryan'
var parseObj= url.parse(urlString, true);
console.log(parseObj);
var params = parseObj.query;
console.log(params);
It is recommended to use: var parsedUrl = new URL(request.url, 'https://your-host'); instead of url.parse
url.parse shouldn't be used in new applications. It is deprecated and could cause some security issues: as stated here

How to pass a variable in an endpoint call in nodejs

I have a variable e.g. var merchanttoken = requestConfig.merchant_connect_token, how do I pass it when making an call to an endpoint e.g
request.get('http://pi.call/v2/{merchanttoken}/info, function)
I need to pass the variable in the url.
I recommend pass it as a POST request instead of GET. Because auth token is sensitive content.
Still if you prefer GET change URL format to this.
http://pi.call/v2/info?merchanttoken={merchanttoken}
You can access it at server side nodejs by
var url = require('url');
var url_parts = url.parse(request.url, true);
var merchanttoken = url_parts.merchanttoken;

Uncaught TypeError: URL is not a constructor using WHATWG URL object support for electron

I am trying to read a file using WHATWG URL object support here
and I am getting this error: Uncaught TypeError: URL is not a constructor
here is my code:
var fs = require("fs");
const { URL } = require('url');
var dbPath = 'file://192.168.5.2/db/db.sqlite';
const fileUrl = new URL(dbPath);
I faced the same issue, then I looked into the url module and found a solution
For Node V6 use,
const URL = require('url').Url;
or
const { Url } = require('url');
If you look into the module, it exports 5 methods one of which is Url, so if you need to access Url, you can use either of the two methods
Are you using Node 6 instead of Node 8?
Node 6
const url = require('url');
const myUrl = url.parse('http://example.com');
const myUrlString = url.format(myUrl);
https://nodejs.org/dist/latest-v6.x/docs/api/url.html#url_url
Node 8
const { URL } = require('url');
const myUrl = new URL('http://example.com');
const myUrlString = myUrl.toString();
https://nodejs.org/dist/latest-v8.x/docs/api/url.html#url_url
Node v10.0.0 and newer (currently Node v19.x)
URL Class
v10.0.0 | The class is now available on the global object.
As mentioned here: Node.js Documentation - Class: URL
So this should work without require('url'):
const myUrl = new URL('http://example.com');
The docs you took this info out are for the node of version 8.4.0.
If it does not work for you, that means that your node is of version 6.11.2. Then, just change the letter case of URL -
const { Url } = require('url');
const myUrl = new Url('http://example.com');
because url module exports Url, not URL.

Why does the "request module" in Node.js accept only the URLs written with protocol?

I want to send a GET request using request module. Here's the code:
var requestModule = require('request');
var url = require('url');
var myUrl = 'www.google.com';
var myUrlObj = url.parse(myUrl);
requestModule(myUrl, myUrlObj , callback);
but it doesn't work because myUrlObj has a null value for its "protocol" attribute.
The same code works when:
var myUrl = 'http://www.google.com'
Why is it so rigid?
Also I tried doing the following to get around this problem:
if ( myUrlObj.protocol == null ) {
myUrl = "http://" + myUrl;
myUrlObj = url.parse(myUrl);
}
But some websites use https, while others use http. So, the above code fails for websites that use https, and the require module throws an exception.
If the URL comes from user input, default to http:// and let them enter a protocol for HTTPS. Encourage them to enter a protocol. Most HTTPS websites will redirect you from the HTTP url to the HTTPS URL. You can make the request module follow redirects using the example here.

Resources