ZijianHe / koa-router

Router middleware for koa.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Router GET with parameter and Nested Routes It's possible?

TrejoCode opened this issue · comments

Hi, have a problem with the routes, it is the following:

All courses: Works

/courses

Specific course: Works

/courses/1

Specific course with Nested Routes: Doesn't work - 404

/courses/1/levels

courses.js

const levels = require('./levels');

const route = new koaRouter({ 
    prefix: '/courses' 
});

route.get('/', koaBody(), async (context) => {
    // Some Logic
    context.body = { "Courses": "All" };
});

route.get('/:id', koaBody(), async (context) => {
    // Some Logic
    context.body = { "Course": "1" };
});

route.get('/:id/levels', levels.routes());

levels.js

const route = new koaRouter({ 
    prefix: '/levels' 
});

route.get('/', koaBody(), async (context) => {
    // Some Logic
    context.body = { "Courses Levels": "All" };
});

Can anybody help me?

Works, using:

route.use('/:id', levelsRoutes.routes());

Example:

const levels = require('./levels');

const route = new koaRouter({ 
    prefix: '/courses' 
});

route.get('/', koaBody(), async (context) => {
    // Some Logic
    context.body = { "Courses": "All" };
});

route.get('/:id', koaBody(), async (context) => {
    // Some Logic
    context.body = { "Course": "1" };
});

// Endpoint for /levels
route.use('/:id', levelsRoutes.routes());

Thank you, seems strange because i need 'repeat' the same route.