Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request not found on '/' ended urls #188

Closed
mortezaom opened this issue Sep 21, 2023 · 4 comments
Closed

Request not found on '/' ended urls #188

mortezaom opened this issue Sep 21, 2023 · 4 comments

Comments

@mortezaom
Copy link

mortezaom commented Sep 21, 2023

Hi @kartikk221
I was working on my API and I created a router with this structure:
image

And when I checked the Rest Client for http://localhost:5000/api/v1 I got the response but for http://localhost:5000/api/v1/ I got "Not Found".

is there any option in hyper-express server to fix this or not?

this is my server object:
image

and these are the responses from the Rest Client:
image
image

@mortezaom mortezaom changed the title Request not found on /' Request not found on '/' ended urls Sep 21, 2023
@kartikk221
Copy link
Owner

Hi, the reason why http://localhost:5000/api/v1 works but http://localhost:5000/api/v1/ does not is because they are fundamentally 2 different URLs. HyperExpress does not automatically route trail slash endings of URLs to their non trail slash routes.

In this scenario, I would recommend adding code in your not found handler to redirect requests ending in trail slashes to their non trail slash counter parts.

Something like below would be a start:

server.set_not_found_handler((request, response) => {
   // Redirect requests ending in trail slashes to non trail slash URL
   if (request.path.endsWith('/')) {
      // Remove the trailing slash from the path of the URL
      const non_trail_slash_url = request.url.replace(request.path, request.path.substring(0, request.path.length - 1));

      // Redirect the request to the non trailing slash URL
      return response.redirect(non_trail_slash_url);
   }
});

@ThinhVu
Copy link

ThinhVu commented Nov 23, 2023

@kartikk221 Does the response.redirect send 301 status to client?
If yes, then remove trail slash before route mapping is better approach.

@kartikk221
Copy link
Owner

The Response.redirect(url) method simply sets the HTTP status code to 302 and writes a location header.

* This method is used to redirect an incoming request to a different url.
*
* @param {String} url Redirect URL
* @returns {Boolean} Boolean
*/
redirect(url) {
if (!this.completed) return this.status(302).header('location', url).send();
return false;
}

Yeah, the best approach would be to remove the trail slash before route mapping and then internally route the requests but the problem is it would be a breaking change for the current version and something that will be kept in consideration for the next major version.

@ThinhVu
Copy link

ThinhVu commented Dec 22, 2023

Yeah, the best approach would be to remove the trail slash before route mapping and then internally route the requests but the problem is it would be a breaking change for the current version and something that will be kept in consideration for the next major version.

I think it's better to keep the same behavior and provide an option to let the user decide whether the trail slash would be removed or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants