koajs / router

Router middleware for Koa. Maintained by @forwardemail and @ladjs.
MIT License
871 stars 176 forks source link

Bad request (400) for route that is a copy of a successful route #98

Closed OriolRF closed 4 years ago

OriolRF commented 4 years ago

Hi, I have a route module file for prefix /tasks with some routes. It has been working properly until I decided to include an extra route (GET /tasks/pending), for which I always get a 400 code result, even if I have it execute the same code of route /tasks/

The order in which I declare it doesn't change this weird behaviour. I can see it executes properly, because if I include console.log with the result of the query I can see the right result, but the client gets a "400 Bad response", even if the result of the query is present in the response body.

The same happened to me before with another route module. In that case I moved the route definition to another place in the file and I solved the problem (without understanding why). But in this case the problem persists

How can I check what's wrong?

Thanks

node.js version: v13.3.0

npm/yarn and version: 6.14.4

@koa/router version: 9.0.1

koa version: 2.12.0

Code sample:

const Router = require('koa-router'); let rows,result;

const router = new Router({ prefix: '/tasks' });

router.get('/', async (ctx, next) => { const query = "SELECT * FROM tareas order by grupo,inicio,previa"; [rows,result] = await ctx.dbPool.query(query); ctx.body = rows; next(); });

router.get('/pending', async (ctx, next) => { const query = "SELECT * FROM tareas order by grupo,inicio,previa"; [rows,result] = await ctx.dbPool.query(query); ctx.body = rows; next(); });

router.get('/:period', async (ctx, next) => {

if (ctx.params.period == "m" ) {
    const mes = (new Date().getMonth()) +1 ; 
    const query = "SELECT * FROM tareas WHERE FIND_IN_SET(" + mes + ",Meses)>0 and Activa='S' order by grupo,inicio,previa";
    [rows,result] = await ctx.dbPool.query(query);
    ctx.body = rows;
} else {
    ctx.response.status = 400;
}

next();

});

... 4 more routes ... ...

Expected Behavior:

Actual Behavior:

Additional steps, HTTP request details, or to reproduce the behavior or a test case:

OriolRF commented 4 years ago

SOLVED. The problem was related to a route definition conflict