GiovanniCardamone / fastify-autoroutes

fastest way to map directories to URLs in fastify
https://giovannicardamone.github.io/fastify-autoroutes/
MIT License
85 stars 12 forks source link

feat: remove trailing `/` to allow query parameters #189

Closed patrickett closed 2 years ago

patrickett commented 2 years ago

I am running into an issue when trying to use a structure like this:

routes/
├─ projects/
│  ├─ index.ts

This will get mapped to www.example.com/projects/

Which means trying to use query parameters with the url www.example.com/projects?case=abc123 will return:

{
    "error": "Not Found",
    "message": "Route GET:/projects?case=abc123 not found",
    "statusCode": 404
}

From my understanding of searching the issue, people tend to lean toward the idea where non trailing slash urls ex. /project provide a resource. Where urls that do provide the trailing slash ex. /project/ are more synonymous with directories.

I think this might cause issues for existing implementations, but I think making it more consistent or some way of expressing when a trailing slash is included is important. Specifically so that in the case above we can use query parameters.

GiovanniCardamone commented 2 years ago

in this way your route match /projects/

if you want to match /projects you should call your file 'project.ts'

like this:

.
├── projects
│   └── {id}.ts
└── projects.ts

or use the option ignoreTrailingSlash https://www.fastify.io/docs/latest/Reference/Server/#ignoretrailingslash

i do not suggest last options

projects and projects/ are different routes:

some examples:

https://ahrefs.com/blog/trailing-slash/#:~:text=A%20trailing%20slash%20is%20a,are%20guidelines%20and%20not%20requirements.

https://www.slimframework.com/docs/v3/cookbook/route-patterns.html

patrickett commented 2 years ago

ignoreTrailingSlash does exactly what I need it to. Thanks!

GiovanniCardamone commented 2 years ago

Thanks to you!