fastify-express icon indicating copy to clipboard operation
fastify-express copied to clipboard

Express wildcard routes override Fastify routes

Open jmory opened this issue 2 years ago • 1 comments

Prerequisites

  • [X] I have written a descriptive issue title
  • [X] 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 avatar Jul 05 '23 10:07 jmory

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.

jmory avatar Jan 08 '25 07:01 jmory