koajs / router

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

post method not allowed #96

Closed CangJL closed 3 years ago

CangJL commented 4 years ago

`const Koa = require('koa'); const Router = require('@koa/router') const Boom = require('boom');

const app = new Koa(); const router = new Router();

router.get('/',ctx => { console.log(ctx) ctx.body = "Hello World"; });

router.get('/user/:id',ctx => { ctx.body = Hello user ${ctx.params.id}; });

//no post method router.post('/user',ctx => { ctx.body = create a user; })

app.use(require('koa-bodyparser')()) app.use(router.routes()) app.use(router.allowedMethods({ throw: true, notImplemented: () => Boom.notImplemented(), methodNotAllowed: () => Boom.methodNotAllowed() }));

app.listen(3000)`

1.router has no post method 2.demo with boom needs update from 'new Boom.notImplemented()' to 'Boom.notImplemented()'

gigenthomas commented 4 years ago

I have the same issue - post fails for a registered route with message "Method Not Allowed"

envelope commented 4 years ago

Which version of koa and @koa/router are you using? I'm not able to reproduce it with the following:

6613974 commented 4 years ago

demo:

const Koa = require('koa');
const router = require('@koa/router')()

const app = new Koa();

router.post('/',ctx =>{
    ctx.body = "112233"
})

app.use(router.routes())
app.use(router.allowedMethods())

app.listen(3000);
3imed-jaberi commented 3 years ago

@CangJL, it's work fine with the latest release (10.x.x). @niftylettuce, please close this issue.

hhaoao commented 3 years ago

Facing beginners using tutorials: https://stackoverflow.com/questions/46747912/post-request-with-parameters-doesnt-work-with-koa-router

post Example:

curl --location --request POST 'http://localhost:5000/posttest/200?id=200'
import Koa from 'koa';
import Router from '@koa/router';

const app = new Koa();
const router = new Router();

router.post('/posttest/:params_id', async (ctx, next) => {
  console.log(ctx.query.id); // 200
  console.log(ctx.params.params_id); // 200
});

app.use(router.routes())
app.use(router.allowedMethods())

app.listen(5000);

There is only one sentence in the document.