Closed CangJL closed 3 years ago
I have the same issue - post fails for a registered route with message "Method Not Allowed"
Which version of koa
and @koa/router
are you using? I'm not able to reproduce it with the following:
koa
v2.13.0@koa/router
9.3.1koa-bodyparser
4.3.0boom
7.3.0demo:
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);
@CangJL, it's work fine with the latest release (10.x.x). @niftylettuce, please close this issue.
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);
`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()'