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

Problem with nested routes using useBase #905

Open
ramsesgarate opened this issue Oct 28, 2024 · 2 comments
Open

Problem with nested routes using useBase #905

ramsesgarate opened this issue Oct 28, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@ramsesgarate
Copy link

ramsesgarate commented Oct 28, 2024

Environment

Node: 22.10.0
h3: 1.13.0
Windows 11

Reproduction

https://stackblitz.com/edit/github-ihqa9f?file=app.ts

Describe the bug

When attempting to set up nested routes using useBase in h3, the route notesRouter.get('/') does not respond as expected, while other routes, such as notesRouter.get('/get'), work correctly.

Steps to Reproduce:

  • Create an application with h3.
  • Define a main route using createRouter and a nested route with useBase for notesRouter.
  • In notesRouter, define two routes: '/get' and '/'.
  • Make a request to '/api/notes/' and observe the behavior.

The route notesRouter.get('/') should respond as expected, similar to notesRouter.get('/get'), allowing for a complete RESTful API structure.

Additional context

import { createApp, createRouter, useBase } from 'h3';

// Init App
export const app = createApp({ debug: true });

// Main Router
const router = createRouter();
router.use(
  '/',
  (event) => `
    <h1>Nested routes</h1>
    <ul>
      <li>Works: <a href="/api/notes/get">/api/notes/get</a></li>
      <li>Not working: <a href="/api/notes/">/api/notes/</a></li>
    </ul>
  `
);
app.use(router);

const notesRouter = createRouter();
router.use('/api/notes/**', useBase('/api/notes', notesRouter.handler));
notesRouter.get('/get', () => '<h1>Nested routes works /api/notes/get</h1><a href="/">Home</a>');
notesRouter.get('/', () => '<h1>Nested routes not working /api/notes/</h1><a href="/">Home</a>');

Logs

No response

@ramsesgarate ramsesgarate added the bug Something isn't working label Oct 28, 2024
@iiio2
Copy link
Contributor

iiio2 commented Nov 3, 2024

@ramsesgarate , I think you have used createRouter twice here. You may try to follow this pattern (https://h3.unjs.io/guide/router#nested-routers).

@snrmwg
Copy link

snrmwg commented Dec 6, 2024

I can observe the same faulty behaviour. The pattern described at https://h3.unjs.io/guide/router#nested-routers also used two createRouter. The "/" route of the child router can only be called at the moment when you add a second "/" to the requested URL (like "/api/notes//" in the demo above).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants