简体   繁体   中英

Passing a URL as a parameter to Node/Express app

I have a simple Node/Express route, as follows:

var app = express();

app.get('gettags/:page', function(request,response)
{
    var thePage = request.params.page;
    ...
    ...
}

The problem I'm having is that if I pass a URL as the parameter, I get a "Cannot GET" error. So, if I call this like:

http://www.mynodeapp.com/gettags/http://www.someurl.com/?withquery=something

I get the "Cannot GET" error. It's been a couple years since I've used Node, so I am probably forgetting something very basic. But I can't get past this, and Googling this issue hasn't turned up anything useful.

Thanks for any help!

Your node code looks fine but for this to work you'll need to URI encode your argument:

var url = 'http://www.mynodeapp.com/gettags/' + encodeURIComponent('http://www.someurl.com/?withquery=something')

//becomes: http://www.mynodeapp.com/gettags/http%3A%2F%2Fwww.someurl.com%2F%3Fwithquery%3Dsomething

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent

The Problem in your code is

/ 's of http://www.someurl.com

so to overcome this use url encoding functions in node such as

encodeURIComponent("Your url goes here")

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