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
routerPath returns url with a trailing slash #4424
Comments
Refs #2782 |
Maybe we should clone the options like #4273 for the |
Agreed! |
Hi @mcollina can I try to work on this isssue ? or someone has already take it ? |
Go for it! |
Can I get some more information about the expected result? |
The second request in this example should return New route added: GET /foo
New route added: HEAD /foo
New route added: HEAD /foo/ <- not sure that we should call onRoute hook for this route
/foo
/foo <- without trailing slash |
Oh okay, I see and understand now thank you for enlightening me 👍🏾 |
you can fix it by using : const fastify = require('fastify')({
ignoreTrailingSlash: true
}) |
'use strict'
const fastify = require('fastify')()
fastify.addHook('onRoute', ({ method, url }) => {
console.log('New route added:', method, url)
})
fastify.register(async (fastify) => {
fastify.get('/', async (request) => {
console.log(request.routerPath)
})
fastify.get('/bar/', async (request) => {
console.log(request.routerPath)
})
}, { prefix: '/foo' })
fastify.ready(() => {
fastify.inject({ method: 'GET', url: '/foo' })
fastify.inject({ method: 'GET', url: '/foo/' })
fastify.inject({ method: 'GET', url: '/foo/bar/' })
}) It shouldn't trim |
I've made some research on the code and made some debuging. |
The problem is inconsistency. Every time fastify registers a new route, the |
no, it returns a route that was registered, not a URL string |
I am interested in contributing to this issue. |
@ivan-tymoshenko I think the code is behaving as it is suppose to. If you would like to get rid of trailing slash from ( |
Prerequisites
Issue
Originally this error comes from fastify-metrics plugin. It relies on two things:
onRoute
hook to set up a metric for the route;routerPath
property to find the route metric from the request context.The problem happens when you use a plugin prefix with the
prefixTrailingSlash
option set toboth
(default) value. In this case, theonRoute
hook will be called only once, sofastify-metrics
will set up only one route, but internally there are two different routes with two differentrouterPath
properties. Sofastify-metrics
will not record metrics for requests with trailing slash.Example:
Output:
If we don't call
onRoute
twice, that means that we want to hide the second route from a user. So from the user's perspective, there should be only one route. If it's true, then we should returnrouterPath
with a trimmed trailing slash.The text was updated successfully, but these errors were encountered: