1

I keep annoyingly on finding examples that only parse JSON sent. I am trying to find an example where a POST comes from a form.

<form class="form-horizontal" role="form" action="/" method="post">
    <input type="text" class="form-control" name="email" placeholder="your@email.here">
    <input type="text" class="form-control" name="password" placeholder="password">
    <button type="submit" class="btn btn-default">Sign in</button>
</form>

As you can see i have fields email and password. I am using koa-router which boasts express style routing but this isn't working:

.post('/', function* () {
    console.log(this.body.email); // <--- undefined
    console.log(this.body.password);
})
basickarl
  • 37,187
  • 64
  • 214
  • 335

1 Answers1

1

You'll want to use a library like the following:

https://github.com/cojs/co-body

let parse = require('co-body');

.post('/', function *(){
  let data = yield parse(this);
  console.log(data.email);
  console.log(data.password);
});
James Moore
  • 1,881
  • 14
  • 18