Home > Software design >  Overlapping routes are not working in Koa.js
Overlapping routes are not working in Koa.js

Time:10-10

When I call /my/abc/create, I always get status 400 because of the first entrypoint. How can I call second endpoint? I prefer not to change the entrypoint order.

var Router = require('koa-router');

var router = Router();


router.get('/my/:path/:id', (ctx) =>{
    if (isNaN(Number(cox.params.id))) { // if not numeric
        ctx.status = 400;
        return;
    }
    console.log('route id')
})
router.get('/my/:path/create', (ctx) =>{ 
    console.log('route create')
})

CodePudding user response:

RESTfully speaking, you wouldn't have a create route at all:

router.get('/my/:path/:id', (ctx) => /* get entity */)
router.post('/my/:path', (ctx) => /* create entity */)
router.patch('/my/:path/:id', (ctx) => /* update entity */)
router.delete('/my/:path/:id', (ctx) => /* delete entity */)
  • Related