首页 文章

POST与koa(nodejs)

提问于
浏览
1

我一直在烦恼地找到只解析发送的JSON的例子 . 我试图找到一个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>

如你所见,我有字段 emailpassword . 我正在使用 koa-router ,它拥有快速样式路由,但这不起作用:

.post('/', function* () {
    console.log(this.body.email); // <--- undefined
    console.log(this.body.password);
})

1 回答

  • 1

    您将要使用如下所示的库:

    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);
    });
    

相关问题