Skip to content

Issues with Requests to the root route (/) #285

@willwill96

Description

@willwill96

Problem

When using wildcards in route matchers of express, the base route is not added as one of the recorded paths. For example, the following server setup will not record requests to http://localhost:3001:

import express from 'express'
import apiSpec from './swagger.json'  assert { type: 'json' };
import swaggerStats from 'swagger-stats'

const app = express()

app.use(swaggerStats.getMiddleware({swaggerSpec: apiSpec, swaggerOnly: true}))

app.get('*', (req, res)=>{
    return res.send('hello world')
})
app.listen(3001, ()=>{
    console.log('listening')
})

Steps to reproduce

(Using node v18.19.0)

  1. Pull down the following repo: https://github.com/willwill96/swagger-stats-repro
  2. Run npm install
  3. Run node server.mjs
  4. Make requests to http://localhost:3001 & http://localhost:3001/other
  5. View http://localhost:3001/swagger-stats/metrics. Note that the path /other is present for some requests, but / is not.

Expected Behavior

I expect to see path="/" as one of the requests in metrics.

Other notes

  1. Interestingly, the only way I've been able to get metrics on the root route to work is by removing the wildcard route handler AND switching to swaggerOnly: false.
  2. I tried commenting out the code here and it started working as I expected, but I didn't test it thoroughly and don't know if removing it might be breaking something else.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions