Arbitrary URL in Express URL parameters? - javascript

Say I have an express get function:
app.get("/api/processing/:type/:link/", () => { ... })
where :link is meant to be an arbitrary, full URL such as https://www.youtube.com/watch?v=ucZl6vQ_8Uo and :type can be one of a few values.
The problem is that whenever I try to use it, I get something like this:
Cannot GET /api/processing/audio/https://www.youtube.com/watch
How can I make sure that the URL is passed as a parameter instead of treated like part of the path?

If you want a full URL to be treated as a single path segment in your URL to match your app.get("/api/processing/:type/:link/", ...) route definition, then you have to encode it properly on the client side so that it actually contains no URL part separators in the encoded piece and thus will match your Express route definition.
You can use encodeURIComponent()for that as in:
const targetURL = 'https://www.youtube.com/watch?v=ucZl6vQ_8Uo';
const requestURL = `https://yourdomain.com/api/audio/${encodeURIComponent(targetURL)}`;
// send your request to requestURL
Then, make the http request to requestURL. Express will handle decoding it properly for you on the server-end of things.
This will generate a requestURL that looks like this:
"https://yourdomain.com/api/audio/https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DucZl6vQ_8Uo"
and you can see in that URL that there are no path separators or query separators, allowing Express to match it properly against your Express route delcaration. You will see that the /, : and ? characters are all escaped so when Express parses the URL into its parts, this whole URL is treated as a single path segment that will match your :link in the route definition.

You can encode the URL so it can be passed as a parameter:
app.get("/api/processing/:type/", (req, res) => {
const link = encodeURIComponent(req.query.link);
// your processing logic here
});
In this example, link is passed as a query parameter rather than part of the path, so you can use req.query.link to access it. When making the request, you would pass the link in the format /api/processing/audio/?link=https://www.youtube.com/watch?v=ucZl6vQ_8Uo.

Related

Force language switch with URL parameter

Is there a way of forcing a language switch by URL parameter using javascript?
I want that when I go to this site 'wwww.google.com/en' he will be in English,
and when I went to 'wwww.google.com/it' he will be in Italian.
I have a button with setLanguage function that does this, but I want it to force it also when I get directly from the URL.
That type of configuration of a single page is typically handled with a query string, not a separate path. Instead of this:
https://www.google.com/en
Do this:
https://www.google.com/?lang=en
The query string data are available in searchParams:
let params = (new URL(document.location)).searchParams;
let lang = params.get('lang');
with window.location.pathname you will get a USVString containing an initial '/' followed by the path of the URL, and to get the first item from the url you can do something like:
const langURI = window.location.pathname.split('/')[1]
You can get info about the USVString here

How to retrieve an URL from the route using #Param

I am writing an image processor proxy, similar to imageproxy, but using NestJS.
I want to declare an endpoint like this: GET /api/trim/http://your.image.url where http://your.image.url is the URL of the image that I want to transform.
In my controller, I would do something like this:
#Get('trim/:imageUrl')
async trimCanvas(
#Param('imageUrl') imageUrl: string,
): Promise<any> {
console.log(imageUrl);
return 'OK';
}
However, if I make a request, the controller is never hit and, instead, I get a default 404. Any ideas on how to make this work?
By default, slashes will not be captured by the url param. You can append a regex in parentheses to your route param to change this behavior. Add a wildcard * to your param, so that it also accepts /:
#Get('trim/:imageUrl(*)')
Try it out in this codesandbox.

How to pass wildcard in vue js post request?

How I can get the current wildcard id and pass it to my $http.post route in vue?
Once I created a quiz information it will return a page with a new url
http://localhost:8000/question/index/quiz/3
Then when I want to do a post route with a name
Route::post('question/store/quiz/{quiz}');
Here is my Vue http request post method
this.$http.post('/question/store/'+ , input).then((response) => {
What will be id that I can pass after the + sign?
Well this is pretty hacky, but it'll work if your URLs are always going to be formatted like that. so what I'm doing here is using vanilla JS to get the URL pathname, parse the string by the / and turn it into an array, then grab the last index.
var locationString = location.pathname
var locationArray = locationString.split('/')
var quizId = locationArray[locationArray.length - 1];
The quizId variable is the wildcard you're looking for
You should note that this is going to break if you ever have any query parameters, such as a URL looking like: /index/quiz/3?v=2842

Passing a query string to a request as an actual string using Node and Express

So, basically what I am doing is scraping a webpage, getting all of the data I want and displaying it on a webpage on my site. When scraping this specific page i need the link within the 'href' tag. However, this particular site doesn't use regular links. Inside the 'href' tag is a query string. My plan was to take what was inside the 'href' and create a url to make my next request, but now when I try to pass the query string into the url, I can not access it in Node via req.params
I want to know if there is a way to maybe pass a query string without the server thinking it is a query string, or will I have to use req.query to take all the params and build the URL again from scratch?
Here are some examples of what I am talking about:
page1.ejs:
some.href = "?variable=bleh"
Server-side handling:
app.get('/display/:string', function(req, res) {
var url = "http://theurlineed.com/" + req.params.string;
});
This code does not work. When i click on the link it tells me it couldn't get /display/?variable=bleh
You need to encode the query string so that it is not treated like a query string in the URL:
some.href = encodeURIComponent("?variable=bleh");
So then your URL will be: /display/%3Fvariable%3Dbleh. As mentioned in the comments, Express will automatically decode the value in req.params.string so it will be the right value.

How to get url from parameter

One of my parameters in the routing is actually an url.
router.get('/api/sitemap/:url', function(req, res)
{
var url = req.params.url;
...
}
How do I allow this to go through when the :url is actually a link like "http://domain.com/file.xml".
I get a 404 error which I understand as it is not linking properly and cannot process as it errors.
Thanks in advance.
Your router returns 404 because it can't recognize the path.
You should either encode the url param as suggested in the comments, or slice it further, as:
.get('/api/site/:domain/: file', cb)
The trouble there is that if you also pass the protocol, you have to match even that.
Don't have a console to try now, but I think you might be able to pass a wildcard:
'/api/sitemap/*'
You would have to parse out the url on your own then, but it's simple:
var url = req.url.substr(14);
(Not sure if it's 13 or14 on the index there, counting on hands since I'm on my mobile :-)).

Categories