2014-07-16 4 views
1

저는 서버에 양식을 구문 분석하기 위해 Express.js 및 body-parser 모듈을 사용했습니다. 그러나 콘텐츠가 수신되면 res.body은 빈 개체로 표시됩니다.Express 폼 구문 분석이 작동하지 않습니다.

app.js : (옥)

var express = require("express"); 
var app = express(); 
var bp = require("body-parser"); 

app.set('views', __dirname + '/views'); 

app.use(bp.json()); 
app.use(express.static(__dirname + '/public')); 

app.get('/', function (req, res) { 
    res.render('index.jade'); 
}); 

app.post('/', function (req, res) { 
    console.log(req.body); 
}); 

app.listen(process.env.PORT || 5000); 

형태 :

form(method="post", action="/", enctype="application/json") 
    input(type="text", name="name", placeholder="input your name") 

왜 그렇게이며 어떻게 고정 할 수있다?

답변

0

bodyparser.json()은 JSON 데이터로 요청을 구문 분석합니다. 당신은 bodyparser.urlencoded()를 사용해야합니다 :

app.use(bodyParser.urlencoded({extended: false})) 

extended: false

중첩 된 값이 처리되지 않음을 의미, 예를 들어, foo[bar]=baz. 중첩 된 값을 지원하려면이 값을 true로 전환 할 수 있습니다.

+0

감사합니다. 시간 제한을 기다리는 중 ... – kyr

관련 문제