简体   繁体   中英

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:

<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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM