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
Related
The url contains all the query strings after the # key
http://localhost:3002/callback#access_token=nQevH_hZSjs3qdOoLNnAIITwqd3lCdkq&expires_in=7200&token_type=Bearer
how do we access the params after #
var url = 'http://localhost:3002/callback#access_token=nQevH_hZSjs3qdOoLNnAIITwqd3lCdkq&expires_in=7200&token_type=Bearer';
function getUrlParameter(name) {
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
var regex = new RegExp('[\\#&]' + name + '=([^&#]*)');
var results = regex.exec(url);
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
};
console.log(getUrlParameter('access_token'));
console.log(getUrlParameter('expires_in'));
console.log(getUrlParameter('token_type'));
Best practice is to use ? instead of #
So your url should be
http://localhost:3002/callback?access_token=nQevH_hZSjs3qdOoLNnAIITwqd3lCdkq&expires_in=7200&token_type=Bearer
Now you can get the query params with below method
var express = require('express');
var app = express();
app.get('/callback', function(req, res){
console.log('access_token: ' + req.query.access_token);
console.log('expires_in: ' + req.query.expires_in);
console.log('token_type: ' + req.query. token_type);
});
app.listen(3000);
Anything after the # isn't sent to the server by the browser.. you can
only parse it if the url is generated or obtained from the server. Then you can use nodes in-built url module to parse symbols in the url
You can use the substring() method:
EDIT: the string you can get from response.body. You have to use body-parser or express.json
let str = "http://localhost:3002/callback#access_token=nQevH_hZSjs3qdOoLNnAIITwqd3lCdkq&expires_in=7200&token_type=Bearer";
let index=str.indexOf("#");
let res = str.substring(index+1);
Output:
$ node server.js
access_token=nQevH_hZSjs3qdOoLNnAIITwqd3lCdkq&expires_in=7200&token_type=Bearer
I'm a beginner with node.js and cheerio and a little help would be awesome :D
I try to scrape the pubg.op.gg website to have two simple elements to show them in the console.
Here is my code:
var url = "https://pubg.op.gg/user/K1uu"
var request = require('request');
var cheerio = require('cheerio');
var cheerioAdv = require('cheerio-advanced-selectors');
request(url, function(err, resp, body) {
var $ = cheerio.load(body);
var playerName = $('.player-summary__name');
var playerNameText = playerName.text();
console.log(playerNameText);
var playerRank = $('.ranked-stats__rating-point');
var playerRankText = playerRank.text();
console.log(playerRankText);
})
I try to have something like this : "Kyuu - 1503"
No problem for the Kyuu value for playernickname but impossible to have the 1503 however the name of the div is correct !
Where is my problem ?
Thanks guys !!
Hey and welcome to StackOverflow!
That website uses AJAX to fetch the ratings, so when the HTML is loaded the ratings are not available and the ranked-stats__rating-point class does not exist yet. If you check it with the browser's developer tools, you can see that it requests 3 additional URLs for the 3 different rating point (the only difference is the queue_size URL param).
https://pubg.op.gg/api/users/59fdce2bdf1b210001a9324d/ranked-stats?season=pc-2018-01&queue_size=1&mode=tpp
https://pubg.op.gg/api/users/59fdce2bdf1b210001a9324d/ranked-stats?season=pc-2018-01&queue_size=2&mode=tpp
https://pubg.op.gg/api/users/59fdce2bdf1b210001a9324d/ranked-stats?season=pc-2018-01&queue_size=4&mode=tpp
You should be able to request the first rating like this:
var url = "https://pubg.op.gg/api/users/59fdce2bdf1b210001a9324d/ranked-stats?season=pc-2018-01&queue_size=1&mode=tpp";
var request = require('request');
request(url, function(err, resp, body) {
var jsonData = JSON.parse(body);
var score = jsonData['stats']['rating'];
console.log(score); // outputs "1520"
} );
However the username is not available from these endpoints, so you need to find another API endpoint for that if you want to fetch these for arbitrary usernames.
Hi korsosa and thanks for your answer !
Yes, there is multiple elements with ranked-stats__rating-point for the name.
There is the result of your code :
var playerRankText = playerRank[1].text();
TypeError: Cannot read property 'text' of undefined
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;
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.
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.