2016-05-14 62 views
1
的NodeJS我

有下面的代碼post請求,當我通過郵遞員使POST請求我得到req.body爲未定義請求體爲空在

POST請求 http://localhost:1702/es

身體:

{ 「IP_A」: 「191.XXXX」, 「PKTS」:34

}

和Content-Type:「application/json」我還使用了application/x-www-form-urlencoded,但得到了相同的結果。

我app.js是

var express = require('express'); 
var es=require('./routes/es'); 
var app = express(); 
app.post('/es',es.postesdata); 
app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({ extended: true })); 

我的文件,其中我收到一個請求體爲null

exports.postesdata=function(req,res){ 

    var body=req.body; 

    console.log(body);//Getting Undefined here 

} 

上午我在這裏做得不對。

回答

5

express運行中間件爲了嘗試:

app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({ extended: true })); 
app.post('/es',es.postesdata); 
+0

得到了我的錯誤,我有app.post第一前app.use這就造成了身體undefined.Thank你 –

+0

我有同樣的問題。我將上面的代碼移到了應用程序的下方,獲得如下初始化: //創建快速應用程序 var app = express(); //解析content-type的請求 - application/x-www-form-urlencoded app.use(bodyParser.urlencoded({extended:true})); //解析content-type -application/json的請求 app.use(bodyParser.json()); – Mohsin