-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
nested router - doesnt work #13696
Comments
The error with
Updating
As there does not seem to be a check for whether the handler is a router, it is effectively useless, as the only route it can handle is it's own file path. There is a logic in h3 to adjust |
For now, you can workaround this with: import { createRouter, useBase } from 'h3';
const router = createRouter();
router.get('/', () => 'Hello World');
export default useBase('/api/test', router.handler); |
but how to work with another route? |
From the Would be nice to be able to create all your routes programmatically rather than using the file-based routes. |
@ahku As of right now you can not. The handler is working only for the file-system path no matter if you use // @/server/middleware/api.ts -- arbitrary file name inside /server/middleware
import { createRouter, defineEventHandler, useBase } from 'h3';
const router = createRouter();
router.get('/hello', defineEventHandler(event => 'Hello'));
router.get('/hello/world', defineEventHandler(event => 'Hello World'));
const handler = useBase('/api', router.handler);
export default (req, res, next) => {
if (!req.originalUrl.startsWith('/api')) {
return next();
}
return handler(req, res);
} Edit, scratch that. // @/server/api/[...].ts -- catch-all route
import { createRouter, defineEventHandler, useBase } from 'h3';
const router = createRouter();
router.get('/hello', defineEventHandler(event => 'Hello'));
router.get('/hello/world', defineEventHandler(event => 'Hello World'));
export default useBase('/api', router.handler); Edit 2, you can not use root ( |
Quick question, is it possible to hook this workaround up with the nuxt/kit addServerMiddleware function? Ideally I want to delegate a route to a h3 router handler. Might be missing something obvious but I havn't had much luck so far. I assume that you would pass addServerMiddleware the route, e.g. 'api/test', then the value of handler would be useBase(, router.handler) as demonstrated above instead of simple just the router itself as I would think the original intent was. I'm not sure how to pass in a catch all route programatically like this. |
@mjrobinson86 Here's an example with express instead of h3 router, but the principle applies. |
@Aareksio thank you! I was missing the full path in useBase as the first argument, I had assumed that passing the route in the call to addServerMiddleware would have been sufficient then using simply useBase('/', router.handler) would have been sufficient. This pointed me in the right direction. :) |
@Aareksio your solution works during development atm. When trying to deploy on hosting i got fallowing error.
|
@aligzl This is likely a separate problem specific to CF. I suggest creating reproduction and raising another issue. |
When will this be fixed? https://nuxt.com/docs/guide/directory-structure/server#using-a-nested-router It seems to still be an example on the Nuxt website Also, we should be able to omit |
the following does register the route // @/server/api/users/[...].ts
import { createRouter, defineEventHandler, useBase } from 'h3';
const router = createRouter();
router.get('', defineEventHandler(event => 'all users'));
router.get('/:id', defineEventHandler(event => 'one user'));
export default useBase('/api/users', router.handler); could it be done like this since // @/server/api/users.ts
import { createRouter, defineEventHandler, useBase } from 'h3';
const router = createRouter();
router.get('', defineEventHandler(event => 'all users'));
router.get('/:id', defineEventHandler(event => 'one user'));
export default useBase('/api/users', router.handler); |
Same as @obust. What would be the recommended Nuxt way to handle /api/users and /api/users/[id] in the same file ? |
Let's track in #18571. |
Environment
Linux
v16.14.2
3.0.0-27460146.2ad93eb
npm@7.17.0
vite
-
-
-
Reproduction
https://stackblitz.com/edit/github-zvowq4-gj7iuh?file=pages/index.vue
Describe the bug
with latest version of nuxt 3 and follow the documentation i cant create nested route
when i'm trying to get data from api i get error:
Additional context
No response
Logs
No response
The text was updated successfully, but these errors were encountered: