fix: Add helpful error messages for invalid wildcard routes in Express v5 #6666
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR Description
🎯 Problem
Express v5 introduced stricter path parsing that requires wildcard routes to have named parameters. Previously valid syntax like
app.all('*')
now throws crypticpath-to-regexp
errors:This creates a poor developer experience when migrating to Express v5, as users get unclear error messages without guidance on how to fix their code.
✅ Solution
This PR enhances Express to catch
path-to-regexp
errors related to missing parameter names and provide clear, actionable error messages that guide developers to the correct Express v5 syntax.Before:
After:
🔧 Changes Made
app.route()
error handling - Catches path-to-regexp errors and provides helpful messages for wildcard syntax issuesapp.use()
error handling - Similar improvements for middleware path validation*
,/*
,/*/something
) while preserving valid syntax (/*splat
)📋 Error Message Features
/*splat
instead of*
or/*
INVALID_WILDCARD_ROUTE
,INVALID_WILDCARD_PATH
)originalError
property🧪 Testing
app.all()
andapp.use()
🎁 Benefits
📚 Related Issues
Fixes the wildcard route error issue described in multiple community reports and Stack Overflow questions regarding Express v5 migration difficulties.
Note: This enhancement only improves error messaging - it doesn't change Express v5's requirement for named wildcard parameters, which is the intended behavior per the migration guide.