#Can't get Nuxt API / H3 router `[...]` to catch index `/` route

10 messages · Page 1 of 1 (latest)

thick skiff
#

Hello.

Original discussion here: #routing message

I'm in Nuxt, and trying to get an H3 router working on an api folder using the following path /server/api/foo/[...].ts – with mixed fortunes:

import {
  createRouter,
  eventHandler,
  useBase, 
  getRouterParams,
} from 'h3'

const router = createRouter()

router

  // these don't work
  .get('', eventHandler(() => {
    return 'nothing'
  }))
  .get('/', eventHandler(() => {
    return 'slash'
  }))

  // these do work
  .get('/test', eventHandler(() => {
    return 'test'
  }))
  .get('/hello/:name', eventHandler(event => {
    const { name } = getRouterParams(event)
    return `Hello ${name}`
  }))

export default useBase('/api/foo', router.handler)

Whilst routes with slugs (/api/foo/test/) work, the index route (/api/foo/) is not picked up by Nitro:

This localhost page can’t be found

No web page was found for the web address: http://localhost:3333/api/foo/

The only workaround I could seem to find was creating a separate index file outside of the catch-all file:

+- api
    +- foo
        +- index.get.ts   <-- /api/foo
        +- [...].ts       <-- /api/foo/*

I've also tried various combinations of brackets, nested brackets, dots, slugs, dots and slugs and brackets, but no luck.

Note that Nuxt /pages a [...].vue file does seem to catch index and slug routes.

Also, I've checked that routing including the index works in h3, and it does:

So this a Nuxt routes oversight, a bug or by design?

⚡️ Minimal H(TTP) framework built for high performance and portability

thick skiff
#

Can't get Nuxt API / H3 router [...] to catch index / route

vale flicker
#

Not a nuxt routes oversight nor a "bug"

#

Simply the design

#

In order for it to match / it must be optional

#

Which is why you need to wrap it in a second pair of square brackets

thick skiff
#

I'll take another look, but when I tried before as far as I remember, it didn't work

vale flicker
#

Did it work?

thick skiff
#

Slammed on end-of-sprint right now, but I will look again 🙂