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

Middleware does not run for '/' route when using base paths #172

Open
Sleavely opened this issue Feb 21, 2021 · 1 comment
Open

Middleware does not run for '/' route when using base paths #172

Sleavely opened this issue Feb 21, 2021 · 1 comment
Assignees

Comments

@Sleavely
Copy link
Contributor

Sleavely commented Feb 21, 2021

When using a base path, the / route doesn't seem to run middleware properly.

I wrote a failing test case using CORS as an example to illustrate:

it('Middleware runs for root path when basepathed', async function() {
  const testApi = require('../index')({
    base: 'base-path'
  })

  testApi.use((req, res, next) => {
    res.cors()
    next()
  })
  testApi.use((err, req, res, next) => {
    res.cors()
    next()
  })
  testApi.get('/', async () => {
    return { status: 'ok' }
  })

  let _event = Object.assign({},event,{ path: '/base-path/' }) // even got a trailing slash and everything :(
  let result = await testApi.run(_event,{})

  expect(result.multiValueHeaders).to.have.property('access-control-allow-origin')
})

The route itself runs and returns { status: 'ok' } but the CORS headers are missing. If the registered route is changed it works as expected:

// ...
 testApi.get('/potato', async () => {
    return { status: 'ok' }
  })
  let _event = Object.assign({},event,{ path: '/base-path/potato' })
// ... this works.

Interestingly, the error middleware runs fine regardless of the route. If you remove the route / and change the event path to something like '/base-path' you'll get a 404 (or 405, don't recall) but the CORS headers will be there

@naorpeled naorpeled self-assigned this Feb 3, 2023
@jmjoyceiv
Copy link

jmjoyceiv commented Jan 31, 2024

Can confirm this is still occurring as of January 2024 / 1.0.3. A fix would be fantastic. Explicitly targeting / in addition to the global call works, e.g.:

api.use('/', apiCommon.corsMiddleware);
api.use(apiCommon.corsMiddleware);

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

3 participants