2015-05-27 66 views
1

我一直煩惱地找到只解析JSON發送的示例。我試圖找到一個POST來自表單的例子。POST與koa(nodejs)

<form class="form-horizontal" role="form" action="/" method="post"> 
    <input type="text" class="form-control" name="email" placeholder="[email protected]"> 
    <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

你要使用圖書館這樣的:

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