Home > OS >  Express routing giving 404 when receving params
Express routing giving 404 when receving params

Time:12-11

I'm working with Express Router, and I'm trying to get some information from params URL, but I cannot make it works as it always returned me a 404.

This is the URL I'm trying to call

http://localhost:8080/api/v1/contr/method?param1=value1&param2=param2

And in my express router I have:

this.router.get("/contr/method", JWT.authenticateJWT, ContrController.method.bind(ContrController));

And I always get

  finalhandler default 404  2m

If I send the request without params the app work as expected:

http://localhost:8080/api/v1/contr/method

CodePudding user response:

If you're building a URL with a URL itself as a parameter, then you need to call encodeURIComponent() on the embedded URL:

let url = "http://localhost:8080/api/v1/contr/method?url="   encodeURIComponent(otherUrl);

This encodes the parameter in a way that will not be confused with the path of the URL during URL parsing. See doc on encodeURIComponent() for more info.

You need to use encodeURIComponent() on any parameter that contains characters that are special when it comes to URL parsing. They will then be encoded in hex such as / which will cause them to not match any of the URL parsing. And, the URL parsing in Express already automatically decodes them for you.

  • Related