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

Express wildcard routes override Fastify routes #113

Open
2 tasks done
jmory opened this issue Jul 5, 2023 · 1 comment
Open
2 tasks done

Express wildcard routes override Fastify routes #113

jmory opened this issue Jul 5, 2023 · 1 comment

Comments

@jmory
Copy link

jmory commented Jul 5, 2023

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.19.2

Plugin version

2.3.0

Node.js version

18.5.0

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

Ventura 13.4

Description

When mixing a Fastify route and an Express route with a wildcard (*), the handler for the Express wildcard route will always be called over the handler for the Fastify route, even if the Fastify route is configured first.

Steps to Reproduce

const Fastify = require('fastify')
const express = require('express')
const router = express.Router()

async function build () {
    const fastify = Fastify()
    fastify.get('/hello-fastify', function (request, reply) {
        reply.send({
            hello: 'fastify'
        })
    })
    await fastify.register(require('@fastify/express'))
    router.get('/hello-express', (req, res) => {
        res.status(201)
        res.json({ hello: 'express' })
    })
    router.get('*', (req, res) => {
        res.status(404)
        res.json({ msg: 'not found'})
    })
    fastify.use(router);
    return fastify
}

build()
    .then(fastify => fastify.listen({ port: 3000 }))
    .catch(console.log)
curl -X GET http://localhost:3000/hello-express    
{"hello":"express"}
curl -X GET http://localhost:3000/hello-fastify   
{"msg":"not found"}

Expected Behavior

When sending a GET request to the /hello-fastify endpoint

curl -X GET http://localhost:3000/hello-fastify  

the handler function provided when calling fastify.get('/hello-fastify, handler function() {...}) should be called and the response should be

{ hello: 'fastify' }

.

@jmory jmory changed the title Mixing Fastify routes and Express wildcard routes Express wildcard routes override Fastify routes Jul 6, 2023
@jmory
Copy link
Author

jmory commented Jan 8, 2025

Thanks for trying to help @UNES97, but the updated code you posted is (apart from the added comments) the same as the code I originally posted. Also, I'm already registering the express middleware after defining all fastify routes in the code I posted in my initial post.

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

1 participant