ZijianHe / koa-router

Router middleware for koa.
MIT License
4.85k stars 407 forks source link

Getting 404 for all router.post response #518

Closed deviantdes closed 4 years ago

deviantdes commented 4 years ago

Have been looking around but could not find any solution regarding router.post. I am not able to get any response from all the post handlers, the responses from the contexts are 404 and does not contain any body.

Here is my html to submit a test post request

  <form action="http://localhost:3000/api/test" enctype="multipart/form-data" method="POST">
    <input type="hidden" name="name" value="test_name" />
    <input type="hidden" name="incomingIP" value="10.20.30.40" />
    <input type="submit" value="submit form" />
  </form>

Here is my router file (routerclass.route.js)

import Router from 'koa-router';

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

router.post('/test',  async (ctx,next) =>{
    console.log(ctx);
});

export default router;

the app.js

import koa from 'koa';
import bodyParser from 'koa-bodyparser';
import routerClass from './routes/routerclass.route';

const app = new koa();

app.use(bodyParser());
app.use(routerClass.routes());
app.use(routerClass.allowedMethods());

And the print out of the ctx

{
  request: {
    method: 'POST',
    url: '/api/test',
    header: {
      host: 'localhost:3000',
      connection: 'keep-alive',
      'content-length': '31',
      'cache-control': 'max-age=0',
      origin: 'http://localhost:3000',
      'upgrade-insecure-requests': '1',
      'content-type': 'multipart/form-data; boundary=----WebKitFormBoundaryuEls9YxGC2Sm2s62',
      'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36',
      'sec-fetch-user': '?1',
      accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
      'sec-fetch-site': 'same-origin',
      'sec-fetch-mode': 'navigate',
      referer: 'http://localhost:3000/test/',
      'accept-encoding': 'gzip, deflate, br',
      'accept-language': 'en-US,en;q=0.9',
      cookie: ''
    }
  },
  response: {
    status: 404,
    message: 'Not Found',
    header: [Object: null prototype] {
      'content-type': 'text/plain; charset=utf-8',
      'content-length': '33'
    }
  },
  app: { subdomainOffset: 2, proxy: false, env: 'development' },
  originalUrl: '/api/test',
  req: '<original node req>',
  res: '<original node res>',
  socket: '<original node socket>'
}

The post API is able to hit, but the ctx that is printed shows the returned response is 404, and there is no body included.

Feels like im missing an await or usage of another koa library somewhere

any help is appreciated.

jonathangaldino commented 4 years ago

I'm with the same issue, but except that is for one route only. All other is ok.

Pryrios commented 4 years ago

The router needs to return something (for example an empty array).

You need to set your ctx.body to something.

tuananh commented 4 years ago

@jonathangaldino @Pryrios i think you all should head over here for better maintained project https://github.com/koajs/router . also it's officially by koa

deviantdes commented 4 years ago

thanks @Pryrios , problem was solved after setting ctx.status = 200.

Will be closing this issue