0

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:

<a href="/display/<%= some.href %>"></a>

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I will try this now.
Aha! It worked. Thank you very very much! +1 Btw, express did automatically decode it. It turned it into a looong string in the URL bar, but server side when I log the param it was how it needed to be.
Awesome! Don't forget to mark my response as the correct answer :)
Will do. Gotta wait for this silly wait time to mark a correct answer. Seems pointless.
Oh yeah, I forgot SO makes you wait a while to be able to do a bunch of things at the beginning. It is weird that marking an answer as the solution is one of those things...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.