Search code examples
node.jspugkoa

nodejs: Koa , form in pug gives undefined


I have a form in pug with just one input

form(action="/" method="POST")
 label Enter URL to shorten
 br
 input(name="url" type="url") 
 button(type="submit") Submit

I use koa-pug to get the input

async function handleForm (ctx) {
   console.log(ctx.request.body);
}

However this logs undefined

I also use koa-body as body parser

app.use(body());

Solution

  • In your form, you are calling your koa backend with a POST method. So to get the body, you need to use something like co-body,

    ...
    const parse = require('co-body');
    ...
    
    async function handleForm (ctx) {
      let body = await parse(ctx);
      console.log(body);
    }