POST与koa(nodejs)
我一直烦恼地find只parsingJSON发送的例子。 我正在试图find一个POST来自一个表单的例子。
<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>
正如你可以看到我有字段的email
和password
。 我正在使用夸耀koa-router
,但这不工作的koa-router
:
.post('/', function* () { console.log(this.body.email); // <--- undefined console.log(this.body.password); })
你会想要使用如下的库:
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); });