How can i properly simplify my url using variables? - node.js

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}`;

Related

How to create a url that opens a tab downloads a file and closes the tab

I want to create an URL that when clicked upon opens a tab, downloads a file, and closes that tab. Do you guys know how to do it?
Following is an example: https://cdn.discordapp.com/attachments/850262728428748830/937385812671209502/vineboom.ogg
I am quite new to this and overwhelmed to know where to start. Can somebody assist me with this?
I tried messing with Anchor tag but that is not the answer. According to my research figured it has something to do with NodeJS and ExpressJS. Still no idea of what to do.
Create require variables as shown and then created a function with whatever name you like, here I am using "onLoad" as the name. This function just checks for the file name in the URL's file parameter specified then tries to find it in the server.
var url_string = window.location; //window.location.href
var url = new URL(url_string);
var file = url.searchParams.get("file");
var dFile = file;
function onLoad() {
var hiddenElement = document.createElement('a');
hiddenElement.href = `${dFile}`;
hiddenElement.target = '_blank';
hiddenElement.download = `${dFile}`;
hiddenElement.click();
close()
}
Make sure to add onLoad function into the body with event listener of "onload"
<!DOCTYPE html>
<body onload="onLoad()">
</body>

My Custom Routing Is Not Working in Codeigniter 3

Hi my custom routing is not working. When i type http://localhost/sitename the default_controller routing is working but when i type http://localhost/sitename/test the browser output is 404 not found. Please help me thank you.
$route['default_controller'] = 'Traffic/test';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['test'] = "Traffic/test";
Please try this code on the routes.php
$route['default_controller'] = 'welcome';
$route['test'] = 'traffic/test';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
I had to rename my default controller php file to lowercase and the controller class name to lowercase and everything started to work.
When CI looks for the default controller file, it searches in lowercase for the file;
if I name my controller file "Traffic/test" instead of "traffic/test"
$route['default_controller'] = 'traffic/test';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['test'] = "traffic/test";
now above code copy and paste your routes.php
Please note that $route['default_controller'] expects a controller not a controller/method pair. The first thing you need to do is change that to $route['default_controller'] = 'traffic';
You may have some success using a controller/method pair, but you may run into trouble in the future as your routing increases in complexity.
also, as someone else already noted, Codeigniter naming and case convention must be followed: Even though the controller filename is uppercase (i.e., Traffic.php) and the controller class is uppercase too (class Traffic extends CI_Controller) whenever you make a reference to the controller such as in the default_controller route, you must go all lowercase.
That said, your correct routing configuration should be:
$route['default_controller'] = 'traffic';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['test'] = "traffic/test";
With this configuration:
Browsing to example.com will load https://example.com/traffic/index
Browsing to example.com/test will load https://example.com/traffic/test
Browsing to any other URI will attempt to load the controller/method pair according to standard Codeigniter routing (i.e., example.com/something/trial will load the something controller and the trial method within the former)

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;

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.

How can I allocate URL objects in 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.

Resources